Created
April 3, 2018 09:50
-
-
Save talkingdotnet/8d45b9f412d9b49e6f48f0dc08e76cf6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Mvc; | |
using System.IO; | |
using System.Net.Http.Headers; | |
namespace Angular5FileUpload.Controllers | |
{ | |
[Produces("application/json")] | |
[Route("api/[controller]")] | |
public class UploadController : Controller | |
{ | |
private IHostingEnvironment _hostingEnvironment; | |
public UploadController(IHostingEnvironment hostingEnvironment) | |
{ | |
_hostingEnvironment = hostingEnvironment; | |
} | |
[HttpPost, DisableRequestSizeLimit] | |
public ActionResult UploadFile() | |
{ | |
try | |
{ | |
var file = Request.Form.Files[0]; | |
string folderName = "Upload"; | |
string webRootPath = _hostingEnvironment.WebRootPath; | |
string newPath = Path.Combine(webRootPath, folderName); | |
if (!Directory.Exists(newPath)) | |
{ | |
Directory.CreateDirectory(newPath); | |
} | |
if (file.Length > 0) | |
{ | |
string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); | |
string fullPath = Path.Combine(newPath, fileName); | |
using (var stream = new FileStream(fullPath, FileMode.Create)) | |
{ | |
file.CopyTo(stream); | |
} | |
} | |
return Json("Upload Successful."); | |
} | |
catch (System.Exception ex) | |
{ | |
return Json("Upload Failed: " + ex.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment