Deleting an uploaded file from the disk

5 posts, 1 answers
  1. Konstantinos
    Konstantinos avatar
    10 posts
    Member since:
    Sep 2011

    Posted 14 Dec 2011 Link to this post

    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

  2. Answer
    Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 19 Dec 2011 Link to this post

    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 >>

  3. DevCraft banner
  4. Konstantinos
    Konstantinos avatar
    10 posts
    Member since:
    Sep 2011

    Posted 09 Jan 2012 Link to this post

    This works fine, thank you :)


    Kind Regards,
    Konstantinos

  5. SchaF
    SchaF avatar
    28 posts
    Member since:
    Jan 2012

    Posted 15 Feb 2012 Link to this post

    What about doing this when our handler implements RadUploadHandler and not IHttpHandler?
  6. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 16 Feb 2012 Link to this post

    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 >>
Back to Top
DevCraft banner