3

I have the following model :

public class FileModel
{
  public byte[] FileData{get;set;}
  public string FileName {get;set;}
}

I have coded a private Web API service which my Web application consumes. When the user uploads files, I convert those files to a byte array, and send a List<FileModel> to my Web API from C# code (not from an HTML page, since my Web API is private from my website), which saves my file and return results.

Web API method:

[HttpPost]
public UploadFiles(List<FileModel> files)
{
// Do work
}

The above code breaks if I upload many large-sized files - the code failed to serialize the large files' FileModels since they exceed the max serializing length.

How do I fix this issue? Is there any other way to upload files to Web API without exposing it to the users?

3

2 Answers 2

2

Here is some solution for that situation.

Your controller action will not accept any parameters as shown on code snippet.

public async Task<HttpResponseMessage> PostByteArrayAsync()
{
       string root = HttpContext.Current.Server.MapPath("~/folder");
       var provider = new MultipartFormDataStreamProvider(root);
       await Request.Content.ReadAsMultipartAsync(provider);
       foreach (var file in provider.FileData)
       {
             var buffer = File.ReadAllBytes(file.LocalFileName);
             // store to db and other stuff
       }
       return Ok();
}

And the code above for front end sample.

UploadData(event) {
        this.setState({ loading: true });
        event.preventDefault();
        let data = new FormData();
        let fileData = document.querySelector('input[type="file"]').files[0];
        data.append("data", fileData);
        let that = this;
        fetch("api/upload", {
            method: "POST",
            "Content-Type": "multipart/form-data",
            "Accept": "application/json",
            body: data
        }).then(function (res) {
            if (res.ok) {
                call('api', 'GET').then(response => { response.error ? response.message : that.props.change(response); that.setState({ loading: false }) });
            }
            else {
                that.setState({ loading: false });
                that.failedMsg();
            }
        })
    }
Sign up to request clarification or add additional context in comments.

1 Comment

hi thanks for the response but I'm not trying to POST file to my Web API directly.I'm posting file to my MVC action controller and making my controller action POST file to my Web API.I don't want my Web API url exposed since its private (i.e. specific to my website)
1

Add this in your web.config file.

<configuration>
   <system.web>
      <httpRuntime maxRequestLength ="1999999"/>
</system.web>
</configuration>

And also increase your content length in MVC config file.

<system.webServer> 
  <security> 
      <requestFiltering> 
         <requestLimits maxAllowedContentLength="1999999999" /> 
      </requestFiltering> 
  </security>
<system.webServer>

maxRequestLength value is in kilobytes.

maxAllowedContentLength value is in bytes.

You can change above size according to your requirements.

7 Comments

Thanks but maxRequestLength is not the cause of my problem.
When I use httpClient.PostAsJsonAsync<List<FileModel>>("API URL",postObj). I return a response of Web API method not found.
But this is just for large files
I think httpClient is unable to serialize my object to Json and sending empty object and hence I'm getting Web API method not found response. This my best guess.
my web.config content length : <requestLimits maxAllowedContentLength="52428800" />
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.