16 Dec 2013

Handling File Uploads from Dropzone.js with ASP.NET MVC

I had a need recently on an MVC project to allow the upload of image files. Having a look around to check if I was still using the best tools for the job, I came across DropzoneJS , which is excellent.

However, the documentation is a little terse and there are no MVC integration guides yet, so let me present a sample Controller Action for anyone who wants to simply see how to use it.


public string UploadFile()
{
 string errorMessage = "";

 foreach (var fileKey in Request.Files.AllKeys)
 {
  var file = Request.Files[fileKey];
  try
  {
   if (file != null)
   {
     YourInputStreamSaveMethod(file.FileName, file.InputStream);
     // Hint, google the phrase 'save inputstream file .net'
   }
  }
  catch (Exception ex)
  {
   errorMessage = ex.Message;
  }
 }

 if (errorMessage != "")
 {
  Response.StatusCode = 418; // Teapot Fail!
 }
 return errorMessage;
}

1 comment:

  1. Hello friend How i can ingrate dorpzone jquery with existing from have many inputs in asp.net mcv

    ReplyDelete

Comments are very welcome but are moderated to prevent spam.

If I helped you out today, you can buy me a beer below. Cheers!