Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all, am trying to create a custom fileuploader sequence to integrate in my cloud storage project. I want it to look like that of Google Drive.

I have submitted the selected files from the fileupload control and able to get all files name but i want to save all files posted to the server.

This is my code

$("#<%=FileUpload1.ClientID %>").change(function () {<br />
                document.getElementById("form1").submit();<br />
                <%= ShowSelected() %>;<br />
            });



Code Behind

VB
Public Function ShowSelected() As String
        Dim totalfiles As Integer = 0

        For Each postedFile As HttpPostedFile In FileUpload1.PostedFiles
            Dim fileName As String = System.IO.Path.GetFileName(postedFile.FileName)
            Session("selected") &= "<hr>" & postedFile.FileName & "</br>"
            Session("files") &= postedFile.FileName & vbCrLf
            lblPostedFiles.Text = Session("selected").ToString
            totalfiles += 1
        Next

        progressbar2.Attributes("max") = totalfiles * 10
        ProgressValue = totalfiles / 10
        Return ""
    End Function

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim count As Integer = 0
        For Each Filename As String In Request.Files
            Dim hFile As HttpPostedFile = TryCast(Request.Files(Filename), HttpPostedFile)

            'Dim f As HttpPostedFile = Request.Files(Filename)
            hFile.SaveAs(Server.MapPath("~/Uploads/") & Filename)
            lblUploaded.Text &= "<hr>" & Filename & "</br>"
            progressbar2.Attributes("value") += ProgressValue
            count += 1
        Next
        lblMsg.Text = String.Format("{0} files have been uploaded successfully.", count)
    End Sub


Pls i need your help to move on. after saving on click of button1 all i get is FileUpload1 as a file with 0 byte
Posted
Comments
ZurdoDev 13-Aug-15 8:57am    
Which file upload control are you using? You generally have to use it's events, not another control's events, to save the files.
[no name] 13-Aug-15 9:40am    
am using the asp.net fileupload control
ZurdoDev 13-Aug-15 9:41am    
Then you could use the OnUploadComplete event.
Richard Deeming 13-Aug-15 15:45pm    
The code you've posted suggests that you don't understand how ASP.NET works.

ASP.NET receives a request from the client. It then executes the server-side code, following a pre-defined life-cycle[^], and finally renders a response to the client.

You cannot call a server-side method from client-side javascript code using a <%= ... %> block. Instead, this calls the server-side method as soon as the block is rendered, and inserts the returned value into the generated HTML document.

When you change a property on a server-side control, the client will not see that change until your server-side code has finished executing and the final HTML document has been sent back. Changing a property value in a loop has the same effect as setting the final value outside of the loop.

I suggest you read some articles about how ASP.NET works - for example, Web Server and ASP.NET Application Life Cycle in Depth[^]
[no name] 14-Aug-15 5:03am    
Thanks for the Criticism.
I know what am doing! just need a little help of which you did not offer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900