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

UploadCompleted

2 Answers 66 Views
Upload
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Aziz
Top achievements
Rank 1
Aziz asked on 27 Jun 2012, 05:33 PM
I am curious how i would set the uploadHandler so once the last bit of the file is uploaded to the server the upload handler calls file.move(target,source) so that i can then move the file to my final location. The reason i need this is due to another service on my server, a filesystem watcher.

Due to the nature of chunked uploading my filesystem watcher goes crazy if I upload directly to the final location. I thought i had this worked out but I don't think its writing the last chunk before moving the file as filesize is always smaller than the original.


public override bool SaveChunkData(string filePath, long position, byte[] buffer, int contentLength, out int savedBytes)
    {
         
        string tempPath;
        tempPath = @"_tmp/_update";
        tempPath = System.Web.HttpContext.Current.Server.MapPath(tempPath);
        if (IsFinalFileRequest())
        {
            File.Move(tempPath, filePath);                                   
        }
        return base.SaveChunkData(tempPath, position, buffer, contentLength, out savedBytes);
    }

2 Answers, 1 is accepted

Sort by
0
Lancelot
Top achievements
Rank 1
answered on 29 Jun 2012, 03:59 PM
Hi Jacob,

The RadUpload control has a number of events at your disposal at different parts of the uploading cycle. This link will bring you to the list of events, and this link will bring you to the events cycle documentation.

In your circumstance I would use the "UploadFinished" event, here is the official description:

UploadFinished - occurs when all files have been processed and the upload finishes. The event handler receives two arguments:
  • The sender argument contains the RadUpload. This argument is of type object, but can be cast to the RadUpload type.
  • An RoutedEventArgs object.
I hope this helps you achieve the results you're looking for.

Good Luck,
Lancelot
0
Tina Stancheva
Telerik team
answered on 29 Jun 2012, 05:39 PM
Hello Jacob,

Looking at your code snippet I noticed that you've called the base.SaveChunkData() method after you've moved the file. However, please note that the base method has to be invoked first as it processes each chunk of the files. So in your case you'll first move the file and then process the last chunk which can cause the described issue. Instead, please try modifying your SaveChunkData implementation like this:
public override bool SaveChunkData(string filePath, long position, byte[] buffer, int contentLength, out int savedBytes)
{
    string tempPath;
    tempPath = @"_tmp/_update";
    tempPath = System.Web.HttpContext.Current.Server.MapPath(tempPath);
    bool result = base.SaveChunkData(tempPath, position, buffer, contentLength, out savedBytes);
    if (IsFinalFileRequest())
    {
        File.Move(tempPath, filePath);                                  
    }
    return result;
}

However, I'm not really sure what exactly is your scenario without looking at your RadUpload definition so I'm not sure if this change will work for you. If it doesn't you can try the approach Lance suggested or  send us more information on your scenario and the RadUpload definition - especially the TargetFolder settings of the control and where it saves the uploaded files before you move them.

Greetings,
Tina Stancheva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
Upload
Asked by
Aziz
Top achievements
Rank 1
Answers by
Lancelot
Top achievements
Rank 1
Tina Stancheva
Telerik team
Share this question
or