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

Deleting an uploaded file from the disk

4 Answers 134 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Konstantinos
Top achievements
Rank 1
Konstantinos asked on 14 Dec 2011, 11:30 AM

Dear Sir/Madam

Because of issues mentioned in another post, I need to create a Remove button on the Silverlight Upload control, which can delete the file after it has been uploaded. The Silverlight control cannot directly delete files from the disk so I will have to call the handler and ask it to make the deletion.

I have written this in the MainPage.xaml stackpanel:


<StackPanel HorizontalAlignment="Right" Margin="3" Orientation="Horizontal" VerticalAlignment="Center" Name="stckpnl1">
[...]
<telerik:RadButton x:Name="RemoveButton" Click="btnRemove_Click" Margin="5" Padding="15 3" telerik:LocalizationManager.ResourceKey="UploadCancel" telerik:StyleManager.Theme="{StaticResource Theme}" Visibility="Collapsed" ToolTipService.ToolTip="Remove" Content="Remove"/>
[...]
</StackPanel>


In the same file, I show the button like this:


<VisualState x:Name="Uploaded">
 <Storyboard>
 [...]
  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="RemoveButton">
   <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="Visible"/>
  </ObjectAnimationUsingKeyFrames>
 [...]


And the code in my Mainpage.xaml.cs file is this:

private void btnRemove_Click(object sender, System.Windows.RoutedEventArgs e)
{
 radUpload.CancelUpload(); // Call the actual Cancel function of RadUpload

 // Ask the handler to delete the file
}


I need the btnRemove_Click function to call the handler and ask it to use File.Delete in order to delete the file which was just uploaded (I only allow single file uploads).

Which is the best way of creating this? All your help is welcome.

Thank you.


Kind Regards,
Konstantinos

4 Answers, 1 is accepted

Sort by
0
Accepted
Tina Stancheva
Telerik team
answered on 19 Dec 2011, 10:43 AM
Hi Konstantinos,

I attached a sample project demonstrating an approach for deleting a file on the server after it has been uploaded with the RadUpload control. Please have a look at the sample project and let me know if it works for you.

Regards,
Tina Stancheva
the Telerik team

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

0
Konstantinos
Top achievements
Rank 1
answered on 09 Jan 2012, 02:24 PM
This works fine, thank you :)


Kind Regards,
Konstantinos

0
SchaF
Top achievements
Rank 1
answered on 15 Feb 2012, 07:39 PM
What about doing this when our handler implements RadUploadHandler and not IHttpHandler?
0
Tina Stancheva
Telerik team
answered on 16 Feb 2012, 08:38 AM
Hello Brandon,

In the sample attached in this thread the RadUpload uses a handler deriving from RadUploadHandler. However the purpose of the solution was to demonstrate how to remove a file after an upload session has completed. And outset an upload session you cannot access the RadUploadHandler. It is designed to respond to request made from the RadUpload control only.

So if you want to use the RadUploadHandler to remove files, this should happen during the upload session, which means that in the same session you'll have to upload the files and as soon as they are uploaded - delete them. If this is what you need to implement, you can override the SaveChunkData method like so:
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);
 
    if (this.IsFinalFileRequest())
    {
        if (this.IsFinalFileRequest())
        {
            this.RemoveFile(filePath);
        }
    }
    return true;
}

But as I am not sure what exactly are the requirements of your scenario I am not sure if this will work. If it doesn't, please elaborate further on what you need to implement with the RadUpload control so that we can better assist you.

Kind regards,
Tina Stancheva
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Upload
Asked by
Konstantinos
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Konstantinos
Top achievements
Rank 1
SchaF
Top achievements
Rank 1
Share this question
or