C
C#2y ago
joy

upload file that have huge size using asp net framework form

hi all, i understand that we can't upload file to a form with size larger than 2GB using asp net (reference from this link https://www.webdavsystem.com/server/documentation/large_files_iis_asp_net/), can i understand.. do we need to upload the file in chunk using JavaScript to pass the content to our controller? i found an article, im not sure if this is the right approach using javascript.. https://www.dotnetbull.com/2018/09/uploading-large-file-chunks-javascript-ajax-mvc-dotnet.html?m=1
16 Replies
Pobiega
Pobiega2y ago
uploading huge files via http isn't recommended in general
joy
joy2y ago
i see.. so it's better to use javascript to upload huge files, is that correct?
Pobiega
Pobiega2y ago
how would javascript help? if you mean by doing axios.PostAsyncor whatever, thats also via http I've never had to implement a "users can upload giant files" thing myself, so I'm no expert on the subject.. but http transfers are a bit fragile and there is no resume functionality or similar
joy
joy2y ago
i see.. actually im not sure what we should use to upload large files, i tried using asp net and upload huge file into the form, the page just not responding for a very long time and end up crash.. can i understand what approach you usually use to upload a file if using http is not good/fragile?
Pobiega
Pobiega2y ago
for a large file? Probably ftp or something ie, something entirely different It'd be interesting to see how youtube handles it, since they are pretty much the masters of "giant file upload" its likely some form of streaming the file directly from the request stream thou
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega2y ago
That sounds like a very good fit.
joy
joy2y ago
thank you so much both! i'll try out the library 😊 👍
joy
joy2y ago
hi @ybear im trying to follow the tusdotnet documentation on github and saw this snippet code, can we use this in .net framework as well?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
joy
joy2y ago
which version of net did you try before for this implementation?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
joy
joy2y ago
hmm.. can i have the snippet code of your implementation?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
In case you did not read the docs: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0 Client Code:
private readonly HttpClient _httpClient;

private static async Task UploadSampleFileAsync()
{
await using var stream = File.OpenRead("./Test.txt");
using var request = new HttpRequestMessage(HttpMethod.Post, "/api/whatever/upload");
using var content = new MultipartFormDataContent
{
{ new StreamContent(stream), "file", "Test.txt" }
};

request.Content = content;

await client.SendAsync(request);
}
private readonly HttpClient _httpClient;

private static async Task UploadSampleFileAsync()
{
await using var stream = File.OpenRead("./Test.txt");
using var request = new HttpRequestMessage(HttpMethod.Post, "/api/whatever/upload");
using var content = new MultipartFormDataContent
{
{ new StreamContent(stream), "file", "Test.txt" }
};

request.Content = content;

await client.SendAsync(request);
}
ServerCode:
[ApiController]
[Route("api/whatever")]
public class FileController : ControllerBase
{
[HttpPost("upload")]
public IActionResult Upload([FromForm] IFormFile file)
{
// code responsible for file processing
return Ok();
}
}
[ApiController]
[Route("api/whatever")]
public class FileController : ControllerBase
{
[HttpPost("upload")]
public IActionResult Upload([FromForm] IFormFile file)
{
// code responsible for file processing
return Ok();
}
}
The field's name in the body when doing the POST, needs to match the binded field in the Controler's Action https://media.discordapp.net/attachments/569261465463160900/1001188081069735936/unknown.png
Upload files in ASP.NET Core
How to use model binding and streaming to upload files in ASP.NET Core MVC.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View