在ASP.NET Boilerplate中上传图像 [英] Uploading image in ASP.NET Boilerplate
本文介绍了在ASP.NET Boilerplate中上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
发布图像时,HttpContext.Current.Request
是null
.
有没有简单的方法可以做到这一点?我在客户端使用 dropzone.js .
Is there any simple way to achieve this? I am using dropzone.js on client side.
使用Web API(ASP.NET Core 2.0)模板的项目是Angular.
Project is Angular with Web API (ASP.NET Core 2.0) template.
[HttpPost]
public HttpResponseMessage UploadJsonFile()
{
HttpResponseMessage response = new HttpResponseMessage();
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
foreach (string file in httpRequest.Files)
{
var postedFile = httpRequest.Files[file];
var filePath = HttpContext.Current.Server.MapPath("~/UploadFile/" + postedFile.FileName);
postedFile.SaveAs(filePath);
}
}
return response;
}
推荐答案
这是我所做的代码,可以正常工作.
This is the code which i had done it is working fine.
public void PostFile(IFormFile file)
{
var uploads = Path.Combine("ROOT PATH FOR THE FILES", "uploads");
if (file.Length > 0)
{
var filePath = Path.Combine(uploads, file.FileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(fileStream);
}
}
}
这篇关于在ASP.NET Boilerplate中上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文