I have file upload project, when the user press ( add file ) and select the desired file, the following function well be executed to save this file on the server
public ActionResult SaveUploadedFile()
{
string fName = DateTime.Now.ToString("yyyyMMddHHmmssffffff");
bool isSavedSuccessfully = true;
foreach (string fileName in Request.Files)
{
HttpPostedFileBase file = Request.Files[fileName];
if (file != null && file.ContentLength > 0)
{
var originalDirectory = new DirectoryInfo(string.Format("{0}Images\PostImages\DeviceImages\", Server.MapPath(@"")));
string pathString = originalDirectory.ToString();
bool isExists = System.IO.Directory.Exists(pathString);
if (!isExists)
System.IO.Directory.CreateDirectory(pathString);
string type = file.FileName.Substring(file.FileName.IndexOf("."), file.FileName.Length – file.FileName.IndexOf("."));
fName = fName + type;
var path = string.Format("{0}\{1}", pathString, fName);
file.SaveAs(path);
}
}
if (isSavedSuccessfully)
{
UploadImagesNames.Add(fName);
return Json(new { Message = fName });
}
else
{
return Json(new { Message = "Error in saving file" });
}
}
As the file is uploaded, a progress bar and delete button is appeared as shown in the image
When the file is uploaded completely, I can press the ( Remove File ) to remove the file, The problem is that when I press the ( Remove file ) button when the file is not completely uploaded, in other words , I need a way to stop the execution of SaveUploadedFile()
function when the ( Remove file ) button is pressed.
How can I solve this problem ?
Thanks
Try using some opensource or commercial products which does this functionality already. Please take a look at Dropzone.js (open source)
Check if the below code helps you if you include it in your cancel controller method.
Declare a fileshare property: FileShare.Delete
using (var fs = new FileStream(fullName, FileMode.Append, FileAccess.Write, FileShare.Delete))
Can you be more clear please. When I should put this code ?
Ahmed Shamel
The problem is that when I press the ( Remove file ) button when the file is not completely uploaded
What happens if you pressed Remove File button?
Take a look at this thread, maybe you could find it useful: http://stackoverflow.com/questions/7729806/how-to-cancel-and-delete-the-uploading-file-in-asp-net-mvc-3