0

From my MVC app I am trying to do a POST request to my Web API app. After receiving the data inside Web API app, when I try to read it like HttpContext.Current.Request.Form["SenderAddress"]; I get an error saying Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream. Below is the code for both sending and receiving:

POST request (from MVC):

var form = new MultipartFormDataContent();
var uri = _domain + "/api/email/send";

//adding all properties to the form
if (model.SenderAddress != "" && model.SenderAddress != null)
    form.Add(new StringContent(model.SenderAddress), "SenderAddress");  
if (model.Recipients.FirstOrDefault() != null)
{
    foreach (var recipient in model.Recipients)
        form.Add(new StringContent(recipient), "Recipients");
}  
if (model.Attachments.FirstOrDefault() != null)
{
    foreach (var attachment in model.Attachments)
    {
        byte[] fileData = null;
        using (var binaryReader = new BinaryReader(attachment.InputStream))
        {
            fileData = binaryReader.ReadBytes(attachment.ContentLength);
        }
        form.Add(new ByteArrayContent(fileData, 0, fileData.Length), "Attachments", attachment.FileName);
    }
}  
HttpResponseMessage response = await client.PostAsync(uri, form);
response.EnsureSuccessStatusCode();
string sd = response.Content.ReadAsStringAsync().Result;  

Receiving data (at Web API):

var temp3 = HttpContext.Current.Request.Form["SenderAddress"];  

Here I get that error. How do I solve it?

Wahid Masud
  • 993
  • 10
  • 35
  • Maybe this help you https://stackoverflow.com/questions/17602845/post-error-either-binaryread-form-files-or-inputstream-was-accessed-before-t?answertab=votes#tab-top – Hossein Jul 25 '18 at 15:47
  • @Hossein Ya I read that too but unfortunately I didn't understand – Wahid Masud Jul 25 '18 at 16:24

1 Answers1

0

i don't understand what this has to do with end of multipart stream but here is the working code:

public async Task<HttpResponseMessage> PostFormData()
    {
        // Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        try
        {
            // Read the form data.
            await Request.Content.ReadAsMultipartAsync(provider);

            // This illustrates how to get the file names.
            foreach (MultipartFileData file in provider.FileData)
            {
                Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                Trace.WriteLine("Server file path: " + file.LocalFileName);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (System.Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }
    }

also checked the tutorial code in asp.net site as well: https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-2

Hossein
  • 3,083
  • 3
  • 16
  • 33