Problems with my custom validation in Upload

1 Answer 12 Views
Upload
SANDRO
Top achievements
Rank 2
Iron
Iron
Iron
SANDRO asked on 08 May 2024, 10:06 PM

Hello, I hope you are well.

I hope you can help me with this problem I have.

I am using the Kendo UI for JQuery Upload.

What I require is to upload an entire folder of TXT files, but I have a routine that validates that the name and content meet certain rules.

I apply these rules within the foreach loop (var file in files), because what is required is that if the file is valid it shows a text that it is valid, otherwise it shows a text that is not valid and for both options to continue processing the next file.

But by marking that file as erroneous, the controller is exited and the view is returned, thus leaving the processing of the rest of the files incomplete.

I share the code of the view and the controller, hoping you can give me some light.

View Code:

<div class="centered">
    @using Kendo.Mvc.UI
    <div>
        <div class="demo-section">
            @(Html.Kendo().Upload()
                .Name("files")
                .Async(a => a
                    .Save("ChunkSave", "Upload")
                    .Remove("Chunk_Upload_Remove", "Upload")
                    .AutoUpload(true)
                    .ChunkSize(11000)

                )
                .Multiple(true) // Enable multiple file selection
                    .Directory(true)
                    .DirectoryDrop(true)
                .Validation(validation =>
                {
                    //validation.MaxFileSize(20000000);
                })
                .Events(events =>
                {
                    events.Upload("onUpload");
                    events.Success("onUploadSuccess");
                    events.Error("onUploadError");
                    events.Select("onSelect");
                })
                .Messages(messages =>
                {
                    messages
                        .Select("Seleccionar archivos")
                        .DropFilesHere("Suelta archivos aquĆ­ para cargarlos")
                        .Remove("Eliminar")
                        .Retry("Reintentar")
                        .StatusFailed("Error")
                        .StatusUploaded("Terminado")
                        .StatusUploading("Cargando")
                        .UploadSelectedFiles("Cargar archivos")
                        .UploadFail("Error al procesar el archivo.")
                        .HeaderStatusUploaded("Terminado");
                })

            )
        </div>
    </div>
</div>

 

Controller Code:

public async Task<ActionResult> ChunkSave(IEnumerable<IFormFile> files, string metaData, string cve)
{

    int status = 0;
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ChunkMetaData));


    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(metaData));
    ChunkMetaData? somemetaData = serializer.ReadObject(ms) as ChunkMetaData;
    string path = String.Empty;

    if (files != null)
    {
        foreach (var file in files)
        {
            cap_dbt controller = new cap_dbt(_context);
            controller.ProcessFile(file, somemetaData.FileName, cve);
            status = controller.ProcessFile(file, somemetaData.FileName, cve);
            if (status == 1)
            {
                Upload(files, somemetaData.FileName);
            }
        }
    }


    if (somemetaData is null)
    {
        throw new Exception("No Metadata!");
    }

    Util.FileResult fileBlob = new Util.FileResult();
    fileBlob.uploaded = somemetaData.TotalChunks - 1 <= somemetaData.ChunkIndex;
    fileBlob.fileUid = somemetaData.UploadUid;
    fileBlob.warnings = Mensajes.msgLoadCsvWarning;
    fileBlob.message = "[" + somemetaData.FileName + "]\t";

    try
    {
        if (!fileBlob.uploaded)
        {
            fileBlob.message += "[ERROR]" + Mensajes.msgLoadCsvError;

            return StatusCode(500, fileBlob);
        }
        else if (status == -1)
        {
            fileBlob.uploaded = false;
            fileBlob.message += "[ERROR] " + Mensajes.msgLoadCsvError;
            throw new Exception(fileBlob.message); // <------- If I remove this line, it continues processing, but it no longer changes the file, it stays with the same name or empty.

        }

        if (fileBlob.warnings.Equals(""))
        {
            fileBlob.message += Mensajes.msgLoadCsvOk;
        }
    }

    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
        return StatusCode(500, fileBlob);
    }
    finally
    {
        Mensajes.resetMsg();
    }

    return Json(fileBlob);
}

The following line was created to manipulate the Upload properties:
Util.FileResult fileBlob = new Util.FileResult();

I know what I can do if my file is not valid:
fileuid=""
success = false

But I see that if success is false it stops processing all the files and exits and goes to view.

1 Answer, 1 is accepted

Sort by
0
Mihaela
Telerik team
answered on 13 May 2024, 06:03 PM

Hi Sandro,

By default, the selected files are uploaded one after the other. For this reason, the uploading process stops when a specified file is invalid. When the Concurrent() option is enabled, the files will start to upload simultaneously, so the uploading process will not stop in case of invalid files:

 @(Html.Kendo().Upload()
                .Name("files")
                .Async(a => a
                    .Save("ChunkSave", "Upload")
                    .Remove("Chunk_Upload_Remove", "Upload")
                    .AutoUpload(true)
                    .ChunkSize(11000)
                    .Concurrent(true)
                )
                ...
)

Would you enable this option and let me know how the behavior changes at your end?


Regards,
Mihaela
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
SANDRO
Top achievements
Rank 2
Iron
Iron
Iron
commented on 14 May 2024, 11:30 PM

Hello.

I tried the proposal that you indicated to me, but notice that it did not work for what is required.

I'll send you a screenshot and I'll explain.
It can be seen that the files are processed, but the messages that I indicated appear are not displayed. The following code in the view is where I specify the messages:

.Messages(messages =>
{
     messages
         .Select("Select files")
         .DropFilesHere("Drop files here to upload")
         .Remove("Remove")
         .Retry("Retry")
         .StatusFailed("Error")
         .StatusUploaded("Finished")
         .StatusUploading("Uploading")
         .UploadSelectedFiles("Upload files")
         .UploadFail("Error processing file.")
         .HeaderStatusUploaded("Finished");
})

It is also seen in the image that the upload process has not finished, since the blue bars are still active and the pause control is still active.

Looking forward to your comments.

Mihaela
Telerik team
commented on 17 May 2024, 01:30 PM

Hi Sandro,

When a specified file is invalid, you can set the "uploaded" property to "false" and return the respective StatusCode and FileResult (similar to the previous condition that checks for !fileBlob.uploaded:

    try
    {
        if (!fileBlob.uploaded)
        {
            fileBlob.message += "[ERROR]" + Mensajes.msgLoadCsvError;

            return StatusCode(500, fileBlob);
        }
        else if (status == -1)
        {
            fileBlob.uploaded = false;
            fileBlob.message += "[ERROR] " + Mensajes.msgLoadCsvError;
            return StatusCode(500, fileBlob);
        }

        if (fileBlob.warnings.Equals(""))
        {
            fileBlob.message += Mensajes.msgLoadCsvOk;
        }
    }

    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
        return StatusCode(500, fileBlob);
    }
    finally
    {
        Mensajes.resetMsg();
    }

    return Json(fileBlob);

I am attaching a runnable sample for your reference. 

Best,

Mihaela

Tags
Upload
Asked by
SANDRO
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Mihaela
Telerik team
Share this question
or