File upload MVC

前端 未结 5 530
一个人的身影
一个人的身影 2020-12-16 02:55

With the following markup in my view:

相关标签:
5条回答
  • 2020-12-16 03:00

    Even I was facing a problem , The value was null in image at

    public ActionResult UploadPlotImadge(HttpPostedFileBase image) 
    

    Earlier I didn't add [AcceptVerbs(HttpVerbs.Post)] which I added. Even after adding it, it didn't work because the second part I was missing, enctype="multipart/form-data", needed to be in the form tag ..

    Now it works for me ....

    0 讨论(0)
  • 2020-12-16 03:08

    try this class and below action and fix folder path in AppSetting.

    config:

       <appSettings>
                <add key="UploadFolerPath" value="..Your folder path" />
       </appSettings>
    

    view:-

    <form action="Account/AddImage" id="form_AddImage" method="post"   enctype="multipart/form-data">
    
                <input type="file" id="Img" name="Img" class="required" />
    
                <input type="submit" value="Upload" id="btnSubmit" />
    
    </form>
    

    Class:-

    public class FileUpload
    {
        public string SaveFileName
        {
            get;
            set;
        }
    
    
        public bool SaveFile(HttpPostedFileBase file, string FullPath)
        {
            string FileName = Guid.NewGuid().ToString();
    
            FileName = FileName + System.IO.Path.GetExtension(file.FileName);
    
            SaveFileName = FileName;
    
            file.SaveAs(FullPath + "/" + FileName);
            return true;
        }
    }
    

    //Post Action

        [HttpPost]
        public ActionResult AddImage(FormCollection Form)
        {
    
            FileUpload fileupload = new FileUpload();
             var image="";
    
            HttpPostedFileBase file = Request.Files["Img"];
    
            if (file.FileName != null && file.FileName != "")
            {
    
                if (upload.ContentLength > 0)
                {
    
                      fileupload.SaveFile(Request.Files["Img"],    Server.MapPath(AppSetting.ReadAppSetting("UploadFolerPath")));
    
                    image = fileupload.SaveFileName;
    
                    // write here your Add/Save function
    
                    return Content(image);
    
    
                }
            }
            else
            {
                       //return....;
            }
    
        }
    
    0 讨论(0)
  • 2020-12-16 03:11

    Try this code:

        public ActionResult Upload()
        {
            foreach (string file in Request.Files)
            {
                var hpf = this.Request.Files[file];
                if (hpf.ContentLength == 0)
                {
                    continue;
                }
    
                string savedFileName = Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory, "PutYourUploadDirectoryHere");
                    savedFileName = Path.Combine(savedFileName, Path.GetFileName(hpf.FileName));
    
                hpf.SaveAs(savedFileName);
            }
    
        ...
        }
    
    0 讨论(0)
  • Not to be picky here or anything, but here's how the code ought to look, as Daniel is missing a few minor details in the code he's supplied...

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult UploadPlotImage(HttpPostedFileBase image)
    {    
        if ( image != null ) 
        {        
            // do something    
        }
    
        return View();
    }
    
    0 讨论(0)
  • 2020-12-16 03:25

    Use HttpPostedFileBase as a parameter on your action. Also, add the AcceptVerb attribute is set to POST.

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Upload(HttpPostedFileBase image)
    {
        if ( image != null ) {
            // do something
        }
        return View();
    }
    

    This code is quite in the spirit/design of ASP.NET MVC.

    0 讨论(0)
提交回复
热议问题