I am writing an analogue of a messenger in which I am trying to attach files to a message. I put all the files in an ObservableCollection, then I create a MultipartFormDataContent and add all the files there, set the correct headers and send the files. My code
private void _SendFile(string ShortUrl, ObservableCollection<LastFilesModel> files)
{
try
{
var url = IpAddress_server ShortUrl;
client = new HttpClient();
var content = new MultipartFormDataContent();
content.Headers.ContentType.MediaType = "multipart/form-data";
content.Headers.Add(@"Content-Length", "2147483648");
int count = 0;
foreach(var file in files)
{
count ;
var stream = new System.IO.FileStream(file.FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
stream.Position = 0;
content.Add(new StreamContent(stream), "file_" count, file.ShortName);
}
var httpMethod = HttpMethod.Post;
var request = new HttpRequestMessage()
{
RequestUri = new Uri(url),
Method = httpMethod,
Content = content,
};
request.Headers.Add("Authorization", "Bearer " MyUser.Token);
var response = client.SendAsync(request).Result;
response.EnsureSuccessStatusCode();
}
catch(Exception ex)
{
var s =ex.Message;
}
}
If I send 1 file then everything is fine or several small ones, but as soon as I send a large file, for example 33 mb, then the following error occurs.
One or more errors occurred. (contentLength < 0)
CodePudding user response:
On the server (nginx)
set the maximum amount of files to be uploaded and removed the ContentLengtn
headers
Dlete code
content.Headers.Add(@"Content-Length", "2147483648");
stream.Position = 0;
CodePudding user response:
The HTTP server is not configured to accept files over a certain size. You'll have to reconfigure it. You didn't specify the server type, so from here you're on you own as to how to solve this problem.