This is a migrated thread and some comments may be shown as answers.

Server cancel upload and return custom error message to client

6 Answers 147 Views
Upload
This is a migrated thread and some comments may be shown as answers.
derrick
Top achievements
Rank 2
derrick asked on 02 Oct 2009, 03:12 PM
Hi,

I've tried everything I can think of to cancel the upload from within the ProcessStream() override and get a custom error on the client.  (I am requiring a security token in the request, and if it doesn't validate I cancel the upload and would like to tell the user why).

On the server I'm setting both .Result and .AddReturnParam.  On the client I'm listening for FileUploaded, UPloadCanceled, and UPloadFailed--only the UploadFailed event is hit.  (Seems like the UploadCancelled should be hit even if cancelled from the server side--is this a bug?) 

Inside UploadFailed, I check e.HandlerData.Message, e.ErrorMessage, and e.HandlerData.CustomData--my message is not in any of those places.

So, I have two questions: 1) am I doing the check and cancelling the download correctly?  2) How do I get a custom error message back to the client when doing a cancel?

    public override void ProcessStream()
    {
        string token = this.Request.Form["token"];
        if (!ValidateToken(token))
        {
          this.CancelRequest();
          this.Result.Add("Error", "Security token is required.  Please login.");
          this.AddReturnParam("Error", "Security token is required.  Please login.");
          return;
        }

Thank you,

DerrickM

6 Answers, 1 is accepted

Sort by
0
Accepted
Ivan
Telerik team
answered on 07 Oct 2009, 10:46 PM
Hi Derrick,

Thank you for your interest in the RadUpload control.

Generally you are on the right way. There are only some small points you should change. Please follow the points below:
  • The UploadCancelled event is a client-side initiated, i.e. you should not track it.
  • The CancelRequest method will not cancel the server-side processing.
  • To cancel the server-side processing you should return some parameters to the client.
  • At the client-side track the FileUploadFailed event to notify the user that a Log-in is required.
Attached you can find a working example related to your case. Please give it a try and let us know if you have more questions.

Best wishes,
Ivan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
derrick
Top achievements
Rank 2
answered on 08 Oct 2009, 10:00 PM
Thank you; that worked perfectly.  :)

There's no way that I would have been able to figure that out on my own.  I don't believe the documentation covers this kind of detail.  (Update 5 return parameters inside the ProcessStream override in order to cancel the upload).

    public override void ProcessStream()
    {
        token = this.Request.Form["token"];
        if (string.IsNullOrEmpty(token) || !ValidateToken(token))
        {
          this.AddReturnParam(RadUploadConstants.ParamNameMessage, "Security token is required. Please login.");

          string fileName = this.Request.Form[RadUploadConstants.ParamNameFileName];
          string filePath = this.GetFilePath(fileName);

          this.AddReturnParam(RadUploadConstants.ParamNameSuccess, false);
          this.AddReturnParam(RadUploadConstants.ParamNameFileIdent, filePath);
          this.AddReturnParam(RadUploadConstants.ParamNameFileName, fileName);
          this.AddReturnParam(RadUploadConstants.ParamNameFilePath, filePath);
          this.AddReturnParam(RadUploadConstants.ParamNameFinalFileRequest, true);
          return;

0
Ivan
Telerik team
answered on 09 Oct 2009, 01:37 AM
Hi derrick,

Thank you for the hint at the missing documentation. We found that whole the branch with the RadUpload API is missing from our online documentation. Regarding your report we updated your Telerik points.

Once again thank you.

Greetings,
Ivan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Jonx
Top achievements
Rank 2
answered on 04 Dec 2010, 08:31 PM
Hello Ivan,
Maybe I did not find the correct entries but I find the radupload documentation still lacks details about how to handle the files server side.

My requirement is the following. I need to :
- upload a file (csv for example)
- import the file content to my database

Where is the correct place to handle the uploaded file. It looks like it's in SaveChunkData.

Here is my server side code:
public override bool SaveChunkData(string filePath, long position, byte[] buffer, int contentLength, out int savedBytes)
{
    bool result = base.SaveChunkData(filePath, position, buffer, contentLength, out savedBytes);
 
    // Checks if this is the last chunk of the file
    if (this.IsFinalFileRequest())
    {
        //if the file is successfully uploaded
        if (result)
        {
            string fileName = this.GetFileName();
 
            ProcessUploadedFile(fileName);
        }
    }
    return result;
}
 
// Logic that will deal with the uploaded files
private void ProcessUploadedFile(string fileName)
{
    // TODO: Add your logic here
}

How do I send back to the client that my file was imported correctly or not?

How do I handle that result correctly on the client?

In case my file processing takes some time, how can I display on the client that the server is processing the file, eventually with progres notification?

Please, explain me how to do and then add it to the documentation...

That would help me a lot.

Thank you for your help,
John.

0
Alex Fidanov
Telerik team
answered on 08 Dec 2010, 10:16 AM
Hello John,

You can send a message to the clien in the SaveChunkData method using the AddReturnFileParam, like so :

if (this.IsFinalUploadRequest())
{
    ...
    this.AddReturnFileParam(RadUploadConstants.ParamNameMessage, "correctly uploaded");
}
The message can be extracted in the FileUploaded event checking the HandlerData property of the arguments.

void upload_FileUploaded(object sender, FileUploadedEventArgs e)
{
    e.HandlerData.Message;
}
To report progress, you could use a BusyIndicator that will be triggers in the FileUploadStarting event and will be stopeed in the FileUploaded.
Please let us know if you have questions on this matter.

Kind regards,
Alex Fidanov
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Jonx
Top achievements
Rank 2
answered on 09 Dec 2010, 12:37 AM
Thank you. That's what I'm doing now...

Please add to the documentation the usage of all the constants in RadUploadConstants...

Thanks,
Best regards,
John.
Tags
Upload
Asked by
derrick
Top achievements
Rank 2
Answers by
Ivan
Telerik team
derrick
Top achievements
Rank 2
Jonx
Top achievements
Rank 2
Alex Fidanov
Telerik team
Share this question
or