Multipart Data Upload

Ravikiran Akidi 21 Reputation points
2020-10-13T16:38:13.447+00:00

Hi,

Am uploading a document to HTTP Trigger Azure function(Version 2). On receiving the request in my function I see the Files section empty and stream is moved to formdata dictionary. Below is the code how am uploading the document, could someone help me why its not populating the stream in IFormFileCollection.


      using (var _httpClient = new HttpClient())  
            {  
                MultipartFormDataContent form = new MultipartFormDataContent();  
                String headerValue = "form-data; name=\"file\"; filename=\"" + fileName + "\"";  
                byte[] bytes = Encoding.UTF8.GetBytes(headerValue);  
                headerValue = Encoding.UTF8.GetString(bytes, 0, bytes.Length);  
                fileContent.Headers.Add("Content-Disposition", headerValue);  
                fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");  
                form.Add(fileContent);  
                form.Add(new StringContent(metadataValue), metadataKey);  
                _httpClient.Timeout = TimeSpan.FromMinutes(15);  
                _httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + bearerToken);  
                logger.LogInformation($"HttpUtils: UploadFileByMultipart() url:{url}, request param: {metadataValue} reference: {traceLogId}");  
                var response = await _httpClient.PostAsync(url, form).Result.Content.ReadAsStringAsync();  
                logger.LogInformation("HttpUtils: UploadFileByMultipart() end");  
                return response;  
            }  

32073-image.png

Other note how do take the stream from the Formdata dictionary which is in string format and convert to stream which I can play around. I tried below and the resultant is a blank doc, corrupting all the stream

        requestHandler.stream = new MemoryStream();  
        var Content = formdata["file"].ToString();  
        var fileContent = new MemoryStream(Encoding.ASCII.GetBytes(Content));  
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JayaC-MSFT 5,606 Reputation points
    2020-10-19T15:26:08.483+00:00

    @Ravikiran Akidi I am sharing a code sample. You may try this code :

    string filePath = "FILE PATH";  
      
    using (var httpClient = new HttpClient())  
    {  
        using (var form = new MultipartFormDataContent())  
        {  
            using (var fs = File.OpenRead(filePath))  
            {  
                using (var streamContent = new StreamContent(fs))  
                {  
                    using (var fileContent = new ByteArrayContent(await streamContent.ReadAsByteArrayAsync()))  
                    {  
                        fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");  
      
                        // "file" parameter name should be the same as the server side input parameter name  
                        form.Add(fileContent, "file", Path.GetFileName(filePath));  
                        HttpResponseMessage response = await httpClient.PostAsync(url, form);  
                    }  
                }  
            }  
        }  
    }  
    

    I tried to read the form data in a function :

                var formdata = await req.ReadFormAsync();  
      
                var image = req.Form.Files["file"];  
    

    Please let me know if this helps. If yes, please "accept the answer" and " up-vote" so that it helps others in the community.

    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.