Error in upload multiple file

2 posts, 0 answers
  1. Joe
    Joe avatar
    57 posts
    Member since:
    Mar 2013

    Posted 26 Jun 2013 Link to this post

    my code works if i select only one file to upload.
    but it will return error if i select more than one files.

        <telerik:RadAsyncUpload runat="server" ID="AsyncUpload_Attachment" OnFileUploaded="AsyncUpload_Attachment_FileUploaded"
         InitialFileInputsCount="1" MaxFileSize="10240000"
         <telerik:RadButton runat="server" ID="RadButton1" Text="Submit your file"
    OnClick="RadButton1_Click"  AutoPostBack="false" OnClientClicked="updatePictureAndInfo" Skin="Default" />
     <script type="text/javascript">
         var $ = $telerik.$;
         function onClientFileUploaded(sender, args) {
         function updatePictureAndInfo() {
             __doPostBack('RadButton1', '');

    Code Behind

    Protected Sub AsyncUpload_Attachment_FileUploaded(ByVal sender As Object, ByVal e As Telerik.Web.UI.FileUploadedEventArgs)
        If Me.AsyncUpload_Attachment.UploadedFiles.Count > 0 Then
        End If
    End Sub

    Private Sub PopulateUploadedFilesList(ByVal uf As RadAsyncUpload)
        Dim filePath As String = System.Configuration.ConfigurationManager.AppSettings("AttachmentPath").ToString()
        Dim fileName As String = ""
        Dim sessionid As Integer = 0
        Dim dc As New dcLRDBDataContext
        If Not IsNothing(HttpContext.Current.Session("SessionID")) Then
            sessionid = HttpContext.Current.Session("SessionID").ToString
        ElseIf Not IsNothing(HttpContext.Current.Session("tmpSID")) Then
            sessionid = HttpContext.Current.Session("tmpSID").ToString
            Dim rnd As New Random()
            Dim tmpSID As Integer 'allow 10 digit only
            tmpSID = rnd.Next(1000, 9999) & DateTime.Now.Hour & DateTime.Now.Minute
            HttpContext.Current.Session("tmpSID") = tmpSID
            sessionid = tmpSID
        End If
        For Each file As UploadedFile In uf.UploadedFiles
            Dim uploadedFileInfo As New UploadedFileInfo(file)
            Dim newRecord1 As New db_Attachment
            newRecord1.FileName = sessionid & "_" & file.FileName
            newRecord1.FileSize = file.ContentLength / 1024
            newRecord1.UploadBy = HttpContext.Current.Session("UserID")
            newRecord1.UploadDate = DateTime.Now
            newRecord1.LastUpdatedOn = DateTime.Now
            newRecord1.LastUpdatedBy = HttpContext.Current.Session("UserID")
            file.SaveAs(filePath & sessionid & "_" & file.FileName)
    End Sub

    it return error when doing th "file.saveAs...." code

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 Jun 2013 Link to this post

    Hi Joe,

    As per this forum thread, When a file is uploaded by the RadAsyncUpload control it is placed in a temporary folder. After a certain time (by default 4 hours) the files from the temporary folder are deleted by the framework. After a postback it is possible that the files may get deleted before your logic is executed which in turn causes the sporadic throwing of exceptions such as file not found.

    Also the server-side OnFileUploaded fires after a file is uploaded and a postback is triggered and this is a looping process which continues until all the selected files are uploaded. If the uploaded count is zero, then this event wont get fired. So you can avoid this AsyncUpload_Attachment.UploadedFiles.Count > 0 statement.

    Please check the following sample code I tried which works fine at my end.

    <telerik:RadAsyncUpload runat="server" ID="AsyncUpload_Attachment" TargetFolder="~/Images/Img/"
        OnFileUploaded="AsyncUpload_Attachment_FileUploaded" PostbackTriggers="RadButton1"
        InitialFileInputsCount="1" MaxFileSize="10240000" MultipleFileSelection="Automatic" />
    <telerik:RadButton runat="server" ID="RadButton1" Text="Submit your file" OnClick="RadButton1_Click"
        AutoPostBack="false" OnClientClicked="updatePictureAndInfo" Skin="Default" />

    Protected Sub AsyncUpload_Attachment_FileUploaded(sender As Object, e As FileUploadedEventArgs)
        Dim targetfolder As String = AsyncUpload_Attachment.TargetFolder
        'set your target folder
        'Your code to handle session and add files info.
        e.File.SaveAs(Path.Combine(Server.MapPath(targetfolder), e.File.FileName))
    End Sub
    Protected Sub RadButton1_Click(sender As Object, e As EventArgs)
    End Sub

  3. DevCraft R3 2016 release webinar banner
Back to Top