I want to upload a file using drag and drop. I wrote the code as shown below, but every time I try to download the file, it shows that the download failed. Can someone tell me where I am going wrong? I want to drag and drop items from an external source and upload them to my folder, but I cannot do this.
For the controller: -
public ActionResult File() { return View(); } /// <summary> /// The max file size in bytes /// </summary> protected int maxRequestLength { get { HttpRuntimeSection section = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection; if (section != null) return section.MaxRequestLength * 1024; // Default Value else return 4096 * 1024; // Default Value } } /// <summary> /// Checks if a file is sent to the server /// and saves it to the Uploads folder. /// </summary> [HttpPost] private void handleFileUpload() { if (!string.IsNullOrEmpty(Request.Headers["X-File-Name"])) { string path = Server.MapPath(string.Format("~/Uploads/{0}", Request.Headers["X-File-Name"])); Stream inputStream = Request.InputStream; FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate); inputStream.CopyTo(fileStream); fileStream.Close(); } }
and to view it: -
<!DOCTYPE html> <html> <head runat="server"> <title>Drag n' Drop File Upload</title> <link href="/Style.css" rel="Stylesheet" /> <style> body { font: 12px Arial; } #dropZone { border-radius: 5px; border: 2px solid #ccc; background-color: #eee; width: 250px; padding: 50px 0; text-align: center; font-size: 18px; color: #555; margin: 50px auto; } #dropZone.hover { border-color: #aaa; background-color: #ddd; } #dropZone.error { border-color: #f00; background-color: #faa; } </style> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script> <script type="text/javascript"> var dropZone; </script> </head> <body> <form id="form1" runat="server"> <div id="dropZone"> Drop File Here to Upload. </div> </form> </body> </html>
source share