Home > Enterprise >  Load large file in SQL Server
Load large file in SQL Server

Time:12-07

I am trying to upload large files (regardless of file type) into SQL Server database.

But when I upload a large one (at least 13.2MB or more) it appears the next error message:

System.IO.IOException: Supplied file with size 13897053 bytes exceeds the maximum of 512000 bytes.

When the user uploads the files I call the next method to save the files into IList<IBrowserFile>.

private IList<IBrowserFile> Files = new List<IBrowserFile>();
private int MaxAllowdFiles = int.MaxValue;
private long MaxSizeFiles = long.MaxValue;

private async Task OnInputFileChanged(InputFileChangeEventArgs e)
    {
        ClearDragClass();
        /*var files = e.GetMultipleFiles();
        foreach(var file in files)
        {
            Files.Add(file);
            Console.WriteLine(Path.GetFullPath(file.Name));
        }*/

        //using var content = new MultipartFormDataContent();

        foreach (var file in e.GetMultipleFiles(MaxAllowdFiles))
        {
            using var f = file.OpenReadStream(MaxSizeFiles);
            using var fileContent = new StreamContent(f);
            fileContent.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType);

            Files.Add(file);
        }
    }

Once the user has uploaded all the files, they click on a button that call the next method to upload it into a database.

private async void Upload()
    {
        List<string>? notUploadFiles = new();
        foreach (var file in Files)
        {
            using Stream s = file.OpenReadStream();
            using MemoryStream ms = new MemoryStream();
            await s.CopyToAsync(ms);
            byte[] fileBytes = ms.ToArray();

            string extn = new FileInfo(file.Name).Extension;

            var addArchivoTarea = new AddArchivoTareaRequestDTO(Tarea.Id, file.Name, fileBytes, extn);
            var successResponse = await HttpTareas.AddArchivoToTareaAsync(addArchivoTarea);

            if (!successResponse)
            {
                notUploadFiles.Add(file.Name);
            }
        }

        if (notUploadFiles.Count > 0)
        {
            Snackbar.Configuration.SnackbarVariant = Variant.Filled;
            Snackbar.Add("The following files could not be uploaded", Severity.Info);

            Snackbar.Configuration.SnackbarVariant = Variant.Outlined;
            foreach (var file in notUploadFiles)
            {
                Snackbar.Add(file, Severity.Error);
            }
            MudDialog.Close(DialogResult.Ok(true));
        }

        Snackbar.Add("All files have been successfully uploaded", Severity.Success);
        MudDialog.Close(DialogResult.Ok(true));
    }

I don't know what I should add or modify to be able to upload large files.
Any suggestions?

CodePudding user response:

According to this

OpenReadStream enforces a maximum size in bytes of its Stream. Reading one file or multiple files larger than 500 KB results in an exception. This limit prevents developers from accidentally reading large files into memory. The maxAllowedSize parameter of OpenReadStream can be used to specify a larger size if required up to a maximum supported size of 2 GB.

so you can have:

 Stream s = file.OpenReadStream (maxAllowedSize :[the value you prefer]);
  • Related