Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello ,
i have a file that is already uploaded from client and saved on server location,
Now if client want to retrieve that file or want to change that file then how?
Please let me know someone.
Thanks
Vimal Kumar Yadav

Model Code

C#
public class UploadFile
   {

       [Required(ErrorMessage = "Please Upload File")]
       [FileExtensions(Extensions = "txt,doc,docx,pdf", ErrorMessage = "Please upload valid file format")]
       [Display(Name = "File")]
       public HttpPostedFileBase fileName { get; set; }


       [Required]
       [Display(Name = "Instrument Used")]
       public string Instrument { get; set; }

       [Required]
       [Display(Name = "Machine Name")]
       public string MachineName { get; set; }

       [Required]
       [Display(Name = "Running RPM")]
       public string machineRPM { get; set; }

       [Required]
       [Display(Name = "Description")]
       public string Description { get; set; }
}


Controller Code

C#
[HttpPost]
      public ActionResult UploadFile(FormCollection formCollection)
      {
          bool sts = ModelState.IsValid;
          if (ModelState.IsValid)
          {
              HttpPostedFileBase file = Request.Files["UploadedFile"];

              //var fileName = Path.GetFileName(file.FileName);
              if (file == null)
              {
                  ModelState.AddModelError("File", "Please Upload Your file");
              }
              else if (file.ContentLength > 0)
              {
                  int MaxContentLength = 1024 * 1024 * 100; //100 MB
                  string[] AllowedFileExtensions = new string[] { ".sdf", ".docx", ".dat", ".pdf", ".txt" };

                  if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
                  {
                      ModelState.AddModelError("File", "Please file of type: " + string.Join(", ", AllowedFileExtensions));

                  }

                  else if (file.ContentLength > MaxContentLength)
                  {
                      ModelState.AddModelError("File", "Your file is too large, maximum allowed size is: " + MaxContentLength + " MB. Please zip your file and upload agail.");
                  }
                  else
                  {
                      //TO:DO
                      var fileName = Path.GetFileName(file.FileName);
                      string fname = User.Identity.Name + "@" + fileName;
                      var path = Path.Combine(Server.MapPath("~/UploadDownloadData/Files"), fname);
                      if (System.IO.File.Exists(path))
                      {
                          //ViewBag.Message = "This file is already exist!";
                          ModelState.AddModelError("File", "This file is already exist!");
                          //TempData["AlertMessage"] = "my alert message";
                          //return RedirectToAction("UploadFile", "UploadDownload");
                      }
                      else
                      {
                          file.SaveAs(path);
                          using (DBClass context = new DBClass())
                          {
                              context.AddParameter("@UserName", User.Identity.Name);
                              context.AddParameter("@InstrumentUsed", formCollection["Instrument"]);
                              context.AddParameter("@MachineDescription", formCollection["MachineName"]);
                              context.AddParameter("@RunningRPM", formCollection["MachineRPM"]);
                              context.AddParameter("@FilePath", path);
                              context.AddParameter("@Description", formCollection["Description"]);
                              context.AddParameter("@UploadDate", DateTime.Now);
                              if (context.ExecuteNonQuery("addUploadData", CommandType.StoredProcedure) == 1)
                              {
                                  ModelState.Clear();
                                  ViewBag.Message = "File uploaded successfully";
                                  return RedirectToAction("Index", "Home");
                              }
                              else
                              {
                                  ModelState.AddModelError("", "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.");
                              }
                          }
                      }
                  }
              }
          }
          return View();
      }


View

HTML
div class="row" style="padding-left:40px">
    <section id="uploadform">
        @using (Html.BeginForm("UploadFile", "UploadDownload", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class=" form-horizontal" role="form">
                <div class="form-group ">
                    @Html.LabelFor(model => model.fileName, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        <input type="file" name="UploadedFile" id="fileToUpload" class="form-control" />
                        <span class="field-validation-error" id="ErrorText" style="color:red"></span>
                    </div>
                </div>

                <div class="form-group ">
                    @Html.LabelFor(model => model.Instrument, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.DropDownList("Instrument", new List<SelectListItem>{
                    new SelectListItem{ Text="Impaqe-Elete", Value = "Impaqe-Elete" },
                     new SelectListItem{ Text="DI-460", Value = "DI-460"},
                      new SelectListItem{ Text="NPAQ", Value = "NPAQ" },
                       new SelectListItem{ Text="Imax-460", Value = "Imax-460" }
                 }, "Select Department", new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Instrument, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group ">
                    @Html.LabelFor(model => model.MachineName, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(model => model.MachineName, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.MachineName, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group ">
                    @Html.LabelFor(model => model.machineRPM, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(model => model.machineRPM, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.machineRPM, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group ">
                    @Html.LabelFor(model => model.Description, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextAreaFor(model => model.Description, 5, 25, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" id="btnSubmit" value="Submit" class=" btn btn-default" @*onclick="javascript:return Postsubmit()"*@ />
                    </div>
                </div>
            </div>
        }
    </section>

</div>
Posted
Updated 9-Dec-15 22:08pm
v2
Comments
Anisuzzaman Sumon 11-Dec-15 10:08am    
Use rich text editor http://asp.richtexteditor.com/
Suvabrata Roy 15-Dec-15 1:49am    
There should be some unique identifier for all the files, Now allow user to download it and if changes then user will re upload it with the help of unique Id.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900