Created
October 5, 2015 22:43
-
-
Save cmatskas/770ce5cde859c834e2b6 to your computer and use it in GitHub Desktop.
File Upload C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost] | |
public async Task<JsonResult> UploadHomeReport(string id) | |
{ | |
try | |
{ | |
foreach (string file in Request.Files) | |
{ | |
var fileContent = Request.Files[file]; | |
if (fileContent != null && fileContent.ContentLength > 0) | |
{ | |
// get a stream | |
var stream = fileContent.InputStream; | |
// and optionally write the file to disk | |
var fileName = Path.GetFileName(file); | |
var path = Path.Combine(Server.MapPath("~/App_Data/Images"), fileName); | |
using (var fileStream = File.Create(path)) | |
{ | |
stream.CopyTo(fileStream); | |
} | |
} | |
} | |
} | |
catch (Exception) | |
{ | |
Response.StatusCode = (int)HttpStatusCode.BadRequest; | |
return Json("Upload failed"); | |
} | |
return Json("File uploaded successfully"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment