[英]This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked
I am trying to browse and upload a file from client to server using Angular Js and WEB API.I used Input file type for user to select file and post the file to WEB API. 我正在尝试使用Angular Js和WEB API浏览并上传文件从客户端到服务器。我使用输入文件类型供用户选择文件并将文件发布到WEB API。 In web API, I am getting following error "This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked." 在Web API中,我收到以下错误“在调用HttpRequest.GetBufferlessInputStream之后,不支持此方法或属性。”
I am using the following code:- 我使用以下代码: -
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");
}
I get this error when i tried to get files from HTTP request... should I update anything in web config?? 当我试图从HTTP请求中获取文件时,我收到此错误...我应该更新web配置中的任何内容吗?
Please help me to resolve this issue.. 请帮我解决这个问题..
try this it work fine for me. 试试这个对我来说很好。
//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);
}
}
I also had the same problem. 我也有同样的问题。 And the solution by @Jean did not work for me. @Jean的解决方案对我不起作用。
I need to upload some CSV file and had to use it in the controller. 我需要上传一些CSV文件,并且必须在控制器中使用它。
In Javascript, I used Fetch API to upload the csv file. 在Javascript中,我使用Fetch API上传csv文件。
But, in the controller, I used this code: 但是,在控制器中,我使用了这段代码:
[HttpPost]
[CatchException]
public bool ImportBundlesFromCsv()
{
var a = Request.Content.ReadAsByteArrayAsync();
//convert to Stream if needed
Stream stream = new MemoryStream(a.Result); // a.Result is byte[]
// convert to String if needed
string result = System.Text.Encoding.UTF8.GetString(a.Result);
// your code
return true;
}
This worked for me. 这对我有用。 Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.