Skip to content

Instantly share code, notes, and snippets.

@cmatskas
Created October 5, 2015 22:43
Show Gist options
  • Save cmatskas/770ce5cde859c834e2b6 to your computer and use it in GitHub Desktop.
Save cmatskas/770ce5cde859c834e2b6 to your computer and use it in GitHub Desktop.
File Upload C#
[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