4

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. In web API, I am getting following error "This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked."
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??

Please help me to resolve this issue..

Hem
  • 91
  • 1
  • 12

2 Answers2

0

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

            }
        }
0

I also had the same problem. And the solution by @Jean did not work for me.

I need to upload some CSV file and had to use it in the controller.

In Javascript, I used Fetch API to upload the csv file.

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!

Srijani Ghosh
  • 3,935
  • 7
  • 37
  • 68