【发布时间】:2018-08-30 09:14:38
【问题描述】:
我正在使用 HttpPostedFileBase 通过 ASP.NET MVC 上传文件。我需要将此发布到 API 调用中,但出现错误
“状态码:403,原因短语:'禁止',版本:1.1,内容: System.Net.Http.StreamContent”。
这是因为我将文件内容作为“application/octet-stream”传递,而 API 调用希望内容类型为“application/vnd.qlik.sense.app”。 互联网上的许多帖子都说 HttpPostedFileBase 是只读的,我们无法更改内容类型。谁能告诉我如何更改 HttpPostedFileBase 的内容类型。这可能吗?
这是我的代码。
[HttpPost]
public ActionResult UploadFile(HttpPostedFile file)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileNameWithoutExtension(file.FileName);
APIcall.Upload(fileName);
}
return RedirectToAction("Index");
}
【问题讨论】:
-
请参阅this answer 以了解如何从
HttpPostedFileBase继承以在构造函数中为ContentType添加setter -
我修改了如下代码,但文件值现在变为空白。 > [HttpPost] public ActionResult Upload(MemoryPostedFile file) { if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileNameWithoutExtension(file.FileName); APICall.Upload(文件名); } return RedirectToAction("Index"); }
标签: c# asp.net-mvc