Return message to user if file already exists
-
Hi all, I am developing an application that allows users to upload/download/delete files. I have it working but I am struggling to add the following function. Check if the file already exists in the specified folder, if the file exists already, return a message telling the user to choose another name. if the file doesn't exist, save the file in the specified folder. This is my controller code:
public ActionResult UploadFiles(int? id)
{
CI cI = db.Database.Find(id);
foreach (string upload in Request.Files)
{if (Request.Files\[upload\].FileName != "") { string path = Server.MapPath("~/App\_Data/uploads/" + id + "/"); string filename = Path.GetFileName(Request.Files\[upload\].FileName); Response.Write(path); Request.Files\[upload\].SaveAs(Path.Combine(path, filename)); } } return View("Upload"); }
The upload view:
@{
ViewBag.Title = "Upload";
}Upload
$(document).ready(function () { $('#btnUploadFile').on('click', function () { var data = new FormData(); var files = $("#fileUpload").get(0).files; // Add the uploaded image content to the form data collection if (files.length > 0) { data.append("UploadedImage", files\[0\]); } // Make Ajax request with the contentType = false, and procesDate = false var ajaxRequest = $.ajax({ type: "POST", url: "", contentType: false, processData: false, data: data }); ajaxRequest.done(function (xhr, textStatus) { @ViewBag.Message }); }); });
@Html.ActionLink("Documents", "Downloads")
-
Hi all, I am developing an application that allows users to upload/download/delete files. I have it working but I am struggling to add the following function. Check if the file already exists in the specified folder, if the file exists already, return a message telling the user to choose another name. if the file doesn't exist, save the file in the specified folder. This is my controller code:
public ActionResult UploadFiles(int? id)
{
CI cI = db.Database.Find(id);
foreach (string upload in Request.Files)
{if (Request.Files\[upload\].FileName != "") { string path = Server.MapPath("~/App\_Data/uploads/" + id + "/"); string filename = Path.GetFileName(Request.Files\[upload\].FileName); Response.Write(path); Request.Files\[upload\].SaveAs(Path.Combine(path, filename)); } } return View("Upload"); }
The upload view:
@{
ViewBag.Title = "Upload";
}Upload
$(document).ready(function () { $('#btnUploadFile').on('click', function () { var data = new FormData(); var files = $("#fileUpload").get(0).files; // Add the uploaded image content to the form data collection if (files.length > 0) { data.append("UploadedImage", files\[0\]); } // Make Ajax request with the contentType = false, and procesDate = false var ajaxRequest = $.ajax({ type: "POST", url: "", contentType: false, processData: false, data: data }); ajaxRequest.done(function (xhr, textStatus) { @ViewBag.Message }); }); });
@Html.ActionLink("Documents", "Downloads")
When you have the target file name ("Path.Combine(path, filename)") use File.Exists to check if it exists File.Exists Method (String) (System.IO)[^] If it does exists you can return Json from your action, something like {success:false}, and in your ajax call you can examine the result of the call and if it is a json object where success==false then you know the upload didn't work so you can show a message, otherwise do what you normally do with the result. You'll maybe want to run through all the files first to check for existing files and return the failure message if any exist and if not run through them again to save them.
-
When you have the target file name ("Path.Combine(path, filename)") use File.Exists to check if it exists File.Exists Method (String) (System.IO)[^] If it does exists you can return Json from your action, something like {success:false}, and in your ajax call you can examine the result of the call and if it is a json object where success==false then you know the upload didn't work so you can show a message, otherwise do what you normally do with the result. You'll maybe want to run through all the files first to check for existing files and return the failure message if any exist and if not run through them again to save them.
Thanks a mill! Got the basic function working using:
System.Diagnostics.Debug.Write(System.IO.File.Exists(Path.Combine(path, filename)) ? "File exists." : "File does not exist.");
You are a star. I know I should be able to find this info by myself but I am very very early in my development so sometimes it is hard to know what to look for. Thanks!