SaveAs throws error as temporary file has been moved to Target Folder already

5 posts, 1 answers
  1. Henry
    Henry avatar
    35 posts
    Member since:
    Mar 2009

    Posted 28 Mar 2010 Link to this post

    Hi,
    I'm have a page called FileManager.aspx that is calling another page called FileUpload.aspx in a RadWindow.

    The FileUpload.aspx (in a RadWindow) has one asp:Button (uxFileUploadReturnButton) that submits the form and has a OnClick event named FileUploadReturnButton_Click .

    In this click event, it iterates through the UploadedFiles (UploadedFileCollection) and does several business logic - one of which is to use the SaveAs method to save the file in a different file name (GUID in my case).

    However, when doing the above, I encountered an error saying that the "temporary file" cannot be found.

    When debugging, I notice that upon reaching my click event handler in my presenter class, the files has already been moved over to the Target Folder - with their original file names.

    I'm not certain where the problem is to achieve using the SaveAs method in my asp.net web application.

    Note: I've registered the Panel which contains the AsynchUpload control to the RadAjaxManager.
    Note: I'm using RadControls version 2010.1 309 in VS 2008 SP1

    FileMangager.aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Dev/AccountLayout.master" AutoEventWireup="true" CodeBehind="FileManager.aspx.cs" Inherits="Dev.FileManager" %> 
    <asp:Content ID="uxFileManagerContentContainerContent" ContentPlaceHolderID="uxAccountLayoutContentContainerContentPlaceHolder" runat="server">  
     
    <asp:Button ID="Button1" runat="server" Text="Upload Files" /> 
     
    <telerik:RadWindow ID="RadWindow2" runat="server"  OpenerElementId="<%# Button1.ClientID %>" NavigateUrl="~/FileUpload/" 
        AutoSize="false" Behaviors="Close" DestroyOnClose="true" KeepInScreenBounds="true" Modal="true" 
        RegisterWithScriptManager="true" Title="Upload File" VisibleStatusbar="false" Width="600" Height="350">  
    </telerik:RadWindow > 
     
    </asp:Content> 
     

    FileUpload.aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Dev/WindowLayout.master" AutoEventWireup="true" CodeBehind="FileUpload.aspx.cs" Inherits="Dev.FileUpload" Theme="Default" %> 
    <asp:Content ID="uxFileUploadContentContainerContent" ContentPlaceHolderID="uxWindowLayoutContentContainerContentPlaceHolder" runat="server">  
        <div id="uxFileUploadContainerWrapperDiv">  
            <div id="uxFileUploadContainerDiv">  
                <asp:Panel ID="uxFileUploadContainerPanel" runat="server">  
                    <div id="uxFileUploadHeaderWrapperDiv">  
                        <div id="uxFileUploadHeaderDiv">  
                            <asp:Panel ID="uxFileUploadBeginUploadHeaderPanel" CssClass="cssFileUploadBeginUploadHeaderPanel" runat="server" Visible="true">  
                                <h1> 
                                    <asp:Label ID="uxFileUploadBeginUploadTitleLabel" CssClass="cssFileUploadBeginUploadTitleLabel" runat="server" Text="Upload to My Files"></asp:Label>      
                                </h1> 
                            </asp:Panel> 
                            <asp:Panel ID="uxFileUploadEndUploadHeaderPanel" CssClass="cssFileUploadEndUploadHeaderPanel" runat="server" Visible="false">  
                                <h1> 
                                    <asp:Label ID="uxFileUploadEndUploadTitleLabel" CssClass="cssFileUploadEndUploadTitleLabel" runat="server" Text="Upload Complete"></asp:Label> 
                                </h1> 
                            </asp:Panel> 
                        </div> 
                    </div> 
                    <div id="uxFileUploadContentWrapperDiv">  
                        <div id="uxFileUploadContentDiv">  
                            <asp:Panel ID="uxFileUploadBeginUploadContentPanel" CssClass="cssFileUploadBeginUploadContentPanel" runat="server" Visible="true">  
                                <telerik:RadProgressManager ID="uxFileUploadRadProgressManager" runat="server" EnableMemoryOptimization="true" RegisterWithScriptManager="true" /> 
                                <div id="uxFileUploadFileListHeaderDiv">  
                                    <div id="uxFileUploadFileNameColumnDiv">  
                                        <asp:Label ID="uxFileUploadFileNameColumnLabel" runat="server" Text="File Name"></asp:Label>      
                                    </div> 
                                    <div id="uxFileUploadRemoveColumnDiv">  
                                        <asp:Label ID="uxFileUploadRemoveColumnLabel" runat="server" Text="Remove?"></asp:Label>      
                                    </div> 
                                </div> 
                                <div id="uxFileUploadFileListContainerDiv" class="cssFileUploadFileListContainerDiv">  
                                    <telerik:RadAsyncUpload ID="uxFileUploadRadAsyncUpload" CssClass="cssFileUploadRadAsyncUpload" runat="server" AutoAddFileInputs="true"   
                                        ControlObjectsVisibility="None" EnableFileInputSkinning="true" FocusOnLoad="true" Localization-Select="Add File" 
                                        ReadOnlyFileInputs="true" OnFileUploaded="FileUploadRadAsyncUpload_FileUploaded" RegisterWithScriptManager="true">  
                                    </telerik:RadAsyncUpload> 
                                    <telerik:RadProgressArea ID="uxFileUploadRadProgressArea" CssClass="cssFileUploadRadProgressArea" runat="server" RegisterWithScriptManager="true" 
                                        ProgressIndicators="FilesCountBar,FilesCount,FilesCountPercent,SelectedFilesCount,CurrentFileName,TimeElapsed,TimeEstimated">  
                                    </telerik:RadProgressArea> 
                                </div>                          
                                <div id="uxFileUploadFileListFooterDiv">  
                                    <div id="uxFileUploadTotalsDiv">  
                                        <asp:Label ID="uxFileUploadTotalLabel" CssClass="cssFileUploadTotalLabel" runat="server" Text="Total:"></asp:Label> 
                                        <asp:Label ID="uxFileUploadFileCountLabel" CssClass="cssFileUploadFileCountLabel" runat="server" Text="0"></asp:Label> 
                                        <asp:Label ID="uxFileUploadUnitFileLabel" CssClass="cssFileUploadUnitFileLabel" runat="server" Text="files,"></asp:Label> 
                                        <asp:Label ID="uxFileUploadFileSizeLabel" CssClass="cssFileUploadFileSizeLabel" runat="server" Text="0"></asp:Label> 
                                        <asp:Label ID="uxFileUploadUnitSizeLabel" CssClass="cssFileUploadUnitSizeLabel" runat="server" Text="MB"></asp:Label> 
                                    </div> 
                                <br /> 
                                </div> 
                                <div id="uxFileUploadUserActionDiv" class="cssFileUploadUserActionDiv">  
                                    <asp:Button ID="uxFileUploadSaveFilesButton" runat="server" Text="Save Uploaded Files" OnClick="FileUploadSaveFilesButton_Click" /> 
                                </div> 
                            </asp:Panel> 
                            <asp:Panel ID="uxFileUploadEndUploadContentPanel" CssClass="cssFileUploadEndUploadContentPanel" runat="server" Visible="false">  
                                <asp:Label ID="uxFileUploadEndUploadMessageLabel" CssClass="cssFileUploadEndUploadMessageLabel" runat="server" Text="Your upload was completed successfully."></asp:Label> 
                                <asp:Button ID="uxFileUploadReturnButton" CssClass="cssFileUploadReturnButton" runat="server" Text="Return to My Files" OnClick="FileUploadReturnButton_Click" /> 
                            </asp:Panel> 
                        </div> 
                    </div> 
                    <div id="uxFileUploadFooterWrapperDiv">  
                        <div id="uxFileUploadFooterDiv">  
                        </div> 
                    </div> 
                </asp:Panel> 
            </div> 
        </div> 
    </asp:Content> 

    FileUpload.aspx.cs

            protected void Page_Load(object sender, EventArgs e)  
            {  
                if (!this.IsPostBack)  
                {  
                    Presenter.ViewInitializedEventHandler(sender, e);  
                }  
                else 
                {  
                    Presenter.ViewLoadedEventHandler(sender, e);  
                }  
            } 

            protected void FileUploadSaveFilesButton_Click(object sender, EventArgs e)  
            {  
                if (FileUploadSaveFilesButtonClickEventHandler != null)  
                {  
                    FileUploadSaveFilesButtonClickEventHandler(sender, e);  
                }  
            } 

    FileUploadPresenter.cs (class library)

            public override void ViewInitializedEventHandler(object sender, EventArgs e)  
            {  
                // View initilization logic.  
                ConfigureAjaxSettings();  
                ConfigureControls();  
                LoadViewData();  
            }  
     
            public override void ViewLoadedEventHandler(object sender, EventArgs e)  
            {  
                // View loaded logic.  
                ConfigureAjaxSettings();  
     
                // Delegate view events to the presenter.  
                ConfigureEventHandlers();  
            }  
     
            private void ConfigureAjaxSettings()  
            {  
                View.WindowLayoutRadAjaxManager.AjaxSettings.AddAjaxSetting(View.FileUploadSaveFilesButton, View.FileUploadBeginUploadContentPanel);  
            }  
     
            private void ConfigureEventHandlers()  
            {  
                View.FileUploadSaveFilesButtonClickEventHandler += new EventHandler<EventArgs>(FileUploadSaveFilesButtonClickEventHandler);  
                View.FileUploadReturnButtonClickEventHandler += new EventHandler<EventArgs>(FileUploadReturnButtonClickEventHandler);  
                View.FileUploadRadAsyncUploadFileUploadedEventHandler += new EventHandler<FileUploadedEventArgs>(FileUploadRadAsyncUploadFileUploadedEventHandler);  
            } 

            private void FileUploadSaveFilesButtonClickEventHandler(object sender, EventArgs e)  
            {  
                string targetFolder = _configurationDefinition.FileUploadTargetFolder;  
                int upperBound = View.FileUploadRadAsyncUpload.UploadedFiles.Count;  
                string[] fileSystemNames = new string[upperBound];  
                string[] uploadedFileNames = new string[upperBound];  
                string[] fileTitles = new string[upperBound];  
                int[] fileSizes = new int[upperBound];  
                string[] fileContentTypes = new string[upperBound];  
                string[] fileExtensions = new string[upperBound];              
                  
                try 
                {  
                    UploadedFileCollection uploadedFiles = View.FileUploadRadAsyncUpload.UploadedFiles;  
                    for (int index = 0; index < uploadedFiles.Count; index++)  
                    {  
                        fileSystemNames[index] = _cryptographyService.GenerateAlphaNumericGUID();  
                        uploadedFileNames[index] = uploadedFiles[index].GetName();  
                        fileTitles[index] = uploadedFiles[index].GetNameWithoutExtension();  
                        fileSizes[index] = uploadedFiles[index].ContentLength;  
                        fileContentTypes[index] = uploadedFiles[index].ContentType;  
                        fileExtensions[index] = uploadedFiles[index].GetExtension();  
                        // Save the file as a GUID file name  
                        uploadedFiles[index].SaveAs(targetFolder + fileSystemNames[index]);  
                    }  
     
                    // Add the files to database  
                    _fileUploadService.AddFiles(fileSystemNames, uploadedFileNames, fileTitles, fileSizes, fileContentTypes, fileExtensions);  
     
                    // Display the File Upload Complete view  
                    ToggleFileUploadViews();  
                }  
                catch 
                {  
                    try 
                    {  
                        // For any errors delete the files that were moved to the Target Folder  
                        foreach (string fileSystemName in fileSystemNames)  
                        {  
                            if (System.IO.File.Exists(targetFolder + fileSystemName))  
                            {  
                                System.IO.File.Delete(targetFolder + fileSystemName);  
                            }  
                        }  
                    }  
                    catch 
                    {  
                        throw;  
                    }  
                }  
            } 

    Error Message

    System.IO.FileNotFoundException was caught
      Message="Could not find file 'C:\\Dev\\App_Data\\TemporaryFiles\\1lsi1z34.y5v'."
      Source="mscorlib"
      FileName="C:\\Dev\\App_Data\\TemporaryFiles\\1lsi1z34.y5v"
      StackTrace:
           at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
           at System.IO.File.Move(String sourceFileName, String destFileName)
           at Telerik.Web.UI.AsyncUploadedFile.SaveAs(String fileName, Boolean overwrite)
           at Dev.FileUploadPresenter.FileUploadSaveFilesButtonClickEventHandler(Object sender, EventArgs e) in C:\Dev\FileUploadPresenter.cs:line 125

    Many Thanks,
    Henry Wu
  2. Henry
    Henry avatar
    35 posts
    Member since:
    Mar 2009

    Posted 29 Mar 2010 Link to this post

    Hi,
    Just to share my experience, to make SaveAs method work properly, ensure that the TargetFolder and TargetPhysicalFolder is set to nothing.

    What happened to me was the TargetFolder was set to a valid path, thus when the asp:Button was clicked and post-back occured and the uploaded files was automatically transfered to TargetFolder. By setting the TargetFolder to nothing, I am ensured that the SaveAs will work correctly as no automatic transfer will take place.


    Thanks,
    Henry Wu

     

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Answer
    Genady Sergeev
    Admin
    Genady Sergeev avatar
    1596 posts

    Posted 30 Mar 2010 Link to this post

    Hello Henry,

    You have received that error because you have tried to save the uploaded files in the submit button click event handler. RadAsyncUpload provides OnFileUpload server side event that you can use to manipulate the uploaded files. After the OnFileUploaded event handler has executed, RadAsyncUpload will check whether there is TargetFolder set, if there is it will move the uploaded files there hence removing them from the TemporaryFolder. And this is the reason for the error that you have encountered. We will add this to the online help.

    Best wishes,
    Genady Sergeev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. HongKyu Min
    HongKyu Min avatar
    1 posts
    Member since:
    Aug 2012

    Posted 08 Mar 2013 Link to this post

    so what? 
    How do i solve this type problems?

    you give sample code that "AutoUploadingImage" to us
    this code occur error like same.
  6. Plamen
    Admin
    Plamen avatar
    2733 posts

    Posted 13 Mar 2013 Link to this post

    Hello,

     
    It is not quite clear what is the issue you are having so please share the code that you are using with RadAsyncUpload and the exact error so we could be more helpful.

    Regards,
    Plamen
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017