File upload MVC

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

With the following markup in my view:

5条回答
  •  星月不相逢
    2020-12-16 03:08

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

    config:

       
                
       
    

    view:-

    
    
                
    
                
    
    
    

    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....;
            }
    
        }
    

提交回复
热议问题