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
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
[HttpPost]
public ActionResult UploadFile(FormCollection formCollection)
{
bool sts = ModelState.IsValid;
if (ModelState.IsValid)
{
HttpPostedFileBase file = Request.Files["UploadedFile"];
if (file == null)
{
ModelState.AddModelError("File", "Please Upload Your file");
}
else if (file.ContentLength > 0)
{
int MaxContentLength = 1024 * 1024 * 100;
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
{
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))
{
ModelState.AddModelError("File", "This file is already exist!");
}
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
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>