SaveAs throw FileNotFoundException when upload multifiles

3 posts, 0 answers
  1. X
    X avatar
    4 posts
    Member since:
    Mar 2013

    Posted 31 Mar 2013 Link to this post

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

    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))
                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. Hristo Valyavicharski
    Hristo Valyavicharski avatar
    975 posts

    Posted 03 Apr 2013 Link to this post


    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;


    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.
  3. DevCraft R3 2016 release webinar banner
  4. X
    X avatar
    4 posts
    Member since:
    Mar 2013

    Posted 03 Apr 2013 Link to this post

    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. 

Back to Top