Newbie: Upload large file from client to server in ASP.NET

G

Greg

I have seen many posts for this same subject, but my project has some
odd requirements. I need to develop an ASP app that will upload a
large file (200 MB+) to a server without any user interaction, meaning
I can's simply put a file input box on the page and let the user
choose the file. I basically need them to click a button and have the
file transferred. I know that this would be a dangerous practice if
anyone could do it, but is it possible in a controlled environment?
Because of the size of the file I have been looking at FTPing it, but
can't figure out how to get client script to start the FTP. I have
also found this code (below) that utilizes a web service, but I don't
see how the client-script would get the file from the client since it
appears to be ASP. Any ideas for this situation would be MUCH
appreciated.

<WebMethod()> PublicFunction UploadFile(ByVal fs()AsByte,ByVal
FlNameAsString)AsString
Try
Dim m As New MemoryStream(fs)
Dim f As New FileStream("c:\tempUploaded\" & FlName,
FileMode.Create)
m.WriteTo(f)
m.Close()
f.Close()
f = Nothing
m = Nothing
Return "File Uploaded"

Catch ex As Exception
Return ex.Message
End Try
End Function

The UploadFile Webmethod accepts a byte array and the name of the
file, the byte array is copied to a memory stream which is written to
a FileStream.

The client code to upload a file,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim f As System.IO.File
Dim fs As System.IO.FileStream
Dim o As New localhost.Service1()
'file to upload
fs = f.Open("c:\upload.pdf", IO.FileMode.Open, IO.FileAccess.Read)
Dim b(fs.Length - 1) As Byte
fs.Read(b, 0, fs.Length)
MsgBox(o.UploadFile(b, "upload.pdf"))
f = Nothing
fs.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
 
J

John Timney \(Microsoft MVP\)

You can use the HTTP classes to write an upload client as a Winexe, there
are examples in the SDK of how to do this using the webclient uploadfile
method, as shown here.

Console.Write("\nPlease enter the URL to post data to : ");
String uriString = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

Console.WriteLine("\nPlease enter the fully qualified path of the
file to be uploaded to the URL");
string fileName = Console.ReadLine();

Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);
// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray =
myWebClient.UploadFile(uriString,"POST",fileName);

// Decode and display the response.
Console.WriteLine("\nResponse Received.The contents of the file
uploaded are: \n{0}",Encoding.ASCII.GetString(responseArray));

You then need to control your server side timouts, and set your
machine.config settings to control the ma file size permitted for uplaods
(look it up - cant recall all the settings)

If you want to resort to FTP, then you can easily script the FTP.EXE that
comes with windows with the /s flag.

--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
----------------------------------------------
 
G

Greg

Thank you for your reply. Your code sample makes sense. How would I
then execute that script on the client from an aspx? Again, I don't
know if this is even possible because of the devious implications, but
the client is on our LAN so I have complete control.

John Timney \(Microsoft MVP\) said:
You can use the HTTP classes to write an upload client as a Winexe, there
are examples in the SDK of how to do this using the webclient uploadfile
method, as shown here.

Console.Write("\nPlease enter the URL to post data to : ");
String uriString = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

Console.WriteLine("\nPlease enter the fully qualified path of the
file to be uploaded to the URL");
string fileName = Console.ReadLine();

Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);
// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray =
myWebClient.UploadFile(uriString,"POST",fileName);

// Decode and display the response.
Console.WriteLine("\nResponse Received.The contents of the file
uploaded are: \n{0}",Encoding.ASCII.GetString(responseArray));

You then need to control your server side timouts, and set your
machine.config settings to control the ma file size permitted for uplaods
(look it up - cant recall all the settings)

If you want to resort to FTP, then you can easily script the FTP.EXE that
comes with windows with the /s flag.

--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
----------------------------------------------

Greg said:
I have seen many posts for this same subject, but my project has some
odd requirements. I need to develop an ASP app that will upload a
large file (200 MB+) to a server without any user interaction, meaning
I can's simply put a file input box on the page and let the user
choose the file. I basically need them to click a button and have the
file transferred. I know that this would be a dangerous practice if
anyone could do it, but is it possible in a controlled environment?
Because of the size of the file I have been looking at FTPing it, but
can't figure out how to get client script to start the FTP. I have
also found this code (below) that utilizes a web service, but I don't
see how the client-script would get the file from the client since it
appears to be ASP. Any ideas for this situation would be MUCH
appreciated.

<WebMethod()> PublicFunction UploadFile(ByVal fs()AsByte,ByVal
FlNameAsString)AsString
Try
Dim m As New MemoryStream(fs)
Dim f As New FileStream("c:\tempUploaded\" & FlName,
FileMode.Create)
m.WriteTo(f)
m.Close()
f.Close()
f = Nothing
m = Nothing
Return "File Uploaded"

Catch ex As Exception
Return ex.Message
End Try
End Function

The UploadFile Webmethod accepts a byte array and the name of the
file, the byte array is copied to a memory stream which is written to
a FileStream.

The client code to upload a file,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim f As System.IO.File
Dim fs As System.IO.FileStream
Dim o As New localhost.Service1()
'file to upload
fs = f.Open("c:\upload.pdf", IO.FileMode.Open, IO.FileAccess.Read)
Dim b(fs.Length - 1) As Byte
fs.Read(b, 0, fs.Length)
MsgBox(o.UploadFile(b, "upload.pdf"))
f = Nothing
fs.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top