【问题标题】:This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked调用 HttpRequest.GetBufferlessInputStream 后不支持此方法或属性
【发布时间】:2018-03-24 12:35:50
【问题描述】:

我正在尝试使用 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 配置中的任何内容吗?

请帮我解决这个问题..

【问题讨论】:

    标签: c# asp.net-web-api httprequest wcf-web-api


    【解决方案1】:

    试试这个对我来说很好用。

            //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);
    
                }
            }
    

    【讨论】:

    • 嗨@hem 下次您提问时,请尝试检查响应是否适合您并接受答案。
    【解决方案2】:

    我也遇到了同样的问题。 @Jean 的解决方案对我不起作用。

    我需要上传一些 CSV 文件并且必须在控制器中使用它。

    在 Javascript 中,我使用 Fetch API 上传 csv 文件。

    但是,在控制器中,我使用了以下代码:

    [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;
            }
    

    这对我有用。希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 2019-04-09
      • 2013-03-23
      • 1970-01-01
      相关资源
      最近更新 更多