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

SaveAs throw FileNotFoundException when upload multifiles

2 Answers 144 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
X
Top achievements
Rank 1
X asked on 31 Mar 2013, 03:58 PM
my codes upload files with RadAsyncUpload (telerik.web.ui version 2012.3.1016.40), when I uploaded single file, the server event FileUploaded worked well. But when I selected multiple files to upload, the FileUploaded event throw FileNotFoundException at SaveAs(...).

I monitored the temporary folder while uploaded multiple files, all files were uploaded and stored in the temporary folder, but after the first one or two uploaded file(s) were successfully saved, the other files disappeared (were deleted). so when the codes ran to SaveAs, it threw FileNotFoundException. I didn't set TemporaryFileExpiration, so it should be default (4 hours).

So, my question is why those files disappeared after the first files were saved? 

the aspx file section:
=========================================
<div id="uploader">
    <telerik:RadAsyncUpload ID="RadAsyncUpload1" runat="server" Culture="zh-CN" EnableEmbeddedSkins="False" 
        EnableInlineProgress="false" TemporaryFolder="~/Temp" Skin="Windows7" OnFileUploaded="RadAsyncUpload1_FileUploaded"
         AllowedFileExtensions="mp3,wav" MultipleFileSelection="Automatic">
    </telerik:RadAsyncUpload>
    <asp:Button ID="Upload" runat="server" ClientIDMode="Static" Text="Upload" Enabled="true" />
    <telerik:RadProgressManager ID="RadProgressManager1" runat="server" RefreshPeriod="10" />
    <telerik:RadProgressArea ID="RadProgressArea1" runat="server" DisplayCancelButton="true" EnableEmbeddedSkins="false"
        Skin="Windows7" Culture="zh-CN">
    </telerik:RadProgressArea>
</div>

the c# code in FileUploaded event:
==========================================
protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
{
        string filewebpath = "Upload/" + System.DateTime.Now.ToString("yyyy-MM-dd") + "/" + ddlWorkSegment.SelectedItem.Text;
        string filelocalpath = Server.MapPath("~/" + filewebpath);
        string fileduration = "";
        MembershipUser u = Membership.GetUser(User.Identity.Name);
 
        // create|check target folder
        if (!System.IO.Directory.Exists(filelocalpath))
        {
            try
            {
                System.IO.Directory.CreateDirectory(filelocalpath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        foreach (UploadedFile uf in RadAsyncUpload1.UploadedFiles)
        {  
            uf.SaveAs(filelocalpath + "\\" + uf.FileName.ToLower(), true);
            // here is codes for inserting file information into database
        }
        // refresh gridview to show file list
        gdvwFileList.DataSourceID = sqldsFileList.ID;
}



2 Answers, 1 is accepted

Sort by
0
Hristo Valyavicharski
Telerik team
answered on 03 Apr 2013, 03:52 PM
Hi,

Probably you are getting FileNotFoundException exception, because you are trying to save file inside folder that does not exist. You set the following path:
string filewebpath = "Upload/" + System.DateTime.Now.ToString("yyyy-MM-dd") + "/" + User.Identity.Name;

~/Upload/yyyy-MM-dd/Username/filename

but actually you don't have such folders yyyy-MM-dd/Username. If you want to store files in such folder structure you will have to create folders first.

FileUploaded event will be fired for every file in RadAsyncUpload1.UploadedFiles collection, so there is no need to put your code inside foreach loop.

All the best,
Hristo Valyavicharski
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.
0
X
Top achievements
Rank 1
answered on 03 Apr 2013, 05:14 PM
Thanks for your reply.

I think the reason is that multiple files will make the OnFileUploaded event fired for each file, and I used foreach loop to process uploaded files. Therefore, those files already had been processed at the first time firing of OnFileUploaded, and the files in temporary folder also were removed. When next time firing, FileNotFoundException was raised.

I modified codes to use e.File instead of "foreach ... radasyncupload1.UploadedFiles".

It seems work well now. 

Tags
AsyncUpload
Asked by
X
Top achievements
Rank 1
Answers by
Hristo Valyavicharski
Telerik team
X
Top achievements
Rank 1
Share this question
or