简体   繁体   English

在调用HttpRequest.GetBufferlessInputStream之后,不支持此方法或属性

[英]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.

相关问题 调用HttpRequest.GetBufferlessInputStream后,不支持此方法 - This method is not supported after HttpRequest.GetBufferlessInputStream has been invoked 在.NET 4.5.2中将.asmx添加到Nancy-在HttpRequest.GetBufferlessInputStream之后不支持 - Adding .asmx to Nancy in .NET 4.5.2 - not supported after HttpRequest.GetBufferlessInputStream 我应该如何使用 HttpRequest.GetBufferlessInputStream? - How should I use HttpRequest.GetBufferlessInputStream? 在Silverlight中调用事件后,是否有理由从事件中删除方法? - Is there a reason to remove a method from an event after it has been invoked in Silverlight? 在.NET中有一种方法可以在调用另一个方法之后但在输入之前自动调用一个方法 - Is there a way in .NET to have a method called automatically after another method has been invoked but before it is entered “使用”后尚未初始化ConnectionString属性 - The ConnectionString property has not been initialized after “using” 在混淆完成后通过反射在c#中查找类/属性的最佳方法是什么? - What is the best method to find a class/property in c# through reflection after obfuscation has been done? Visual C#在调用Click事件后立即将其删除 - Visual C# removing a Click event directly after it has been invoked C#当另一个类中的静态方法被调用时,如何通知一个类(简单的是/否)? - C# How to notify a class (simple true/false) when a static method in another class has been invoked? 填充ListView之后,设置它的SelectedIndex属性 - Set the SelectedIndex property of a ListView just after it has been populated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM