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

Error in upload multiple file

1 Answer 129 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 26 Jun 2013, 09:34 AM

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"
     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" />
 
 <script type="text/javascript">
          //<![CDATA[
     var $ = $telerik.$;
 
     function onClientFileUploaded(sender, args) {
     }
 
     function updatePictureAndInfo() {
         __doPostBack('RadButton1', '');
     }
         //]]>
         </script>

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
        PopulateUploadedFilesList(AsyncUpload_Attachment)
    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
 
    Else
        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)
        UploadedFiles.Add(uploadedFileInfo)
 
        Dim newRecord1 As New db_Attachment
        dc.db_Attachments.InsertOnSubmit(newRecord1)
 
        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")
 
        dc.SubmitChanges()
 
        file.SaveAs(filePath & sessionid & "_" & file.FileName)
 
    Next
    dc.Dispose()
End Sub

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












1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 26 Jun 2013, 12:26 PM
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 asp.net 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.

ASPX:
<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" />

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

Thanks,
Shinu.
Tags
AsyncUpload
Asked by
Joe
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or