gpt4 book ai didi

c# - 调用 HttpRequest.GetBufferlessInputStream 后不支持此方法或属性

转载 作者:行者123 更新时间:2023-11-30 18:16:20 25 4
gpt4 key购买 nike

我正在尝试使用 Angular Js 和 WEB API 浏览文件并将其从客户端上传到服务器。我使用输入文件类型供用户选择文件并将文件发布到 WEB API。在 Web API 中,出现以下错误“调用 HttpRequest.GetBufferlessInputStream 后不支持此方法或属性。”
我正在使用以下代码:-

     public IHttpActionResult UploadForm()
{
HttpResponseMessage response = new HttpResponseMessage();
var httpRequest = System.Web.HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
foreach (string file in httpRequest.Files)
{
var postedFile = httpRequest.Files[file];
var filePath = System.Web.HttpContext.Current.Server.MapPath("~/UploadFile/" + postedFile.FileName);
postedFile.SaveAs(filePath);
}
}
return Json("Document Saved");
}

当我尝试从 HTTP 请求中获取文件时出现此错误...我应该更新 Web 配置中的任何内容吗?

请帮我解决这个问题..

最佳答案

试试这个对我来说效果很好。

        //get the root folder where file will be store
string root = HttpContext.Current.Server.MapPath("~/UploadFile");

// Read the form data.
var provider = new MultipartFormDataStreamProvider(root);
await Request.Content.ReadAsMultipartAsync(provider);

if (provider.FileData.Count > 0 && provider.FileData[0] != null)
{
MultipartFileData file = provider.FileData[0];

//clean the file name
var fileWithoutQuote = file.Headers.ContentDisposition.FileName.Substring(1, file.Headers.ContentDisposition.FileName.Length - 2);

//get current file directory on the server
var directory = Path.GetDirectoryName(file.LocalFileName);

if (directory != null)
{
//generate new random file name (not mandatory)
var randomFileName = Path.Combine(directory, Path.GetRandomFileName());
var fileExtension = Path.GetExtension(fileWithoutQuote);
var newfilename = Path.ChangeExtension(randomFileName, fileExtension);

//Move file to rename existing upload file name with new random filr name
File.Move(file.LocalFileName, newfilename);

}
}

关于c# - 调用 HttpRequest.GetBufferlessInputStream 后不支持此方法或属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46711871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com