File Upload ASP.NET MVC 3.0

后端 未结 21 998
无人共我
无人共我 2020-11-22 01:09

(Preface: this question is about ASP.NET MVC 3.0 which was released in 2011, it is not about ASP.NET Core 3.0 which was released in 2019)

I want to

21条回答
  •  终归单人心
    2020-11-22 01:52

    I am giving you the simple and easy method to understand and learn.

    First you have to write the following code in your .Cshtml file.

    
    

    then in your controller put following code:

    if (i > 0) {
        HttpPostedFileBase file = Request.Files["Image"];
    
    
        if (file != null && file.ContentLength > 0) {
            if (!string.IsNullOrEmpty(file.FileName)) {
                string extension = Path.GetExtension(file.FileName);
    
                switch ((extension.ToLower())) {
                    case ".doc":
                        break;
                    case ".docx":
                        break;
                    case ".pdf":
                        break;
                    default:
                        ViewBag.result = "Please attach file with extension .doc , .docx , .pdf";
                        return View();
                }
    
                if (!Directory.Exists(Server.MapPath("~") + "\\Resume\\")) {
                    System.IO.Directory.CreateDirectory(Server.MapPath("~") + "\\Resume\\");
                }
    
                string documentpath = Server.MapPath("~") + "\\Resume\\" + i + "_" + file.FileName;
                file.SaveAs(documentpath);
                string filename = i + "_" + file.FileName;
                result = _objbalResume.UpdateResume(filename, i);
                Attachment at = new Attachment(documentpath);
    
                //ViewBag.result = (ans == true ? "Thanks for contacting us.We will reply as soon as possible" : "There is some problem. Please try again later.");
            }
        } else {
            ...
        }
    }
    

    For this you have to make BAL and DAL layer as per your Database.

提交回复
热议问题