Join 244,309 ASP.NET Programmers for FREE! Get instant access to thousands of ASP.NET experts, tutorials, code snippets, and more! There are 855 people online right now. Registration is fast and FREE... Join Now!
1. I want to let end user to fill-in an Excel file, which contains two columns, file description and file location on client side;
2. I want to let end user to upload the Excel file to my web server, then I want my web server to find the files specified in Excel file and upload them into web server.
Well will they be uploading the document from a web application or from a Windows application, that changes your options quite a bit
I am thinking seriously about whether writing a client side program to upload file is better. But I do not know whether it is better because of my limited experience. Each time file upload size is big, may be several hundred M bytes, so I suspect the capability of uploading from browser.
I would do it from the browser. If you are going to be uploading big files, let the server to the heavy lifting. ASP.Net has an upload file control and it is very simple to implement.
protected void cmdUploadFile_Click(object sender, EventArgs e) { string strFileNameOnServer = uplTheFile.PostedFile.FileName; string strBaseLocation = @"C:\Inetpub\wwwroot\TempFolder"; // This is the location on your server where you want the files to be uploaded
if (string.IsNullOrEmpty(strFileNameOnServer)) { lblInformation.Text = "Error - a file name must be specified."; return; }
I'm working on an application where part of the functionality required is the ability to upload files to a web server. I created a Web Service on the web server in which I break the files into a byte array and put the file back together on the server then save it.
I haven't noticed any performance issues when doing it this way, and at times she uploads 400MB+ worth of files.
Here's the WebMethod on the web service
csharp
[WebMethod] public string UploadFile(byte[] fileBytes, string uploadFile, string uploadPath) { try { //new memory stream, pass the byte array MemoryStream memoryStream = new MemoryStream(fileBytes);
//new filestream pointing to the upload folder, pass also the file being uploaded // to name the resulting file FileStream fileStream = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(uploadPath) + uploadFile, FileMode.Create);
//use WriteTo to write the files byte array to the stream memoryStream.WriteTo(fileStream);
//close & clean up memoryStream.Close(); fileStream.Close(); fileStream.Dispose();
And the method on the Windows application looks likie this
csharp
public static void UploadImage(ref string filename, ref string directory) { try { // get the exact file name from the path String file = System.IO.Path.GetFileName(filename);
// create an instance fo the web service {REMOVED}.Core.Uploader.FileUploader service = new {REMOVED}.Core.Uploader.FileUploader();
// get the file information form the selected file FileInfo info = new FileInfo(filename);
// get the length of the file to see if it is possible // to upload it (with the standard 4 MB limit) long size = info.Length; double length = Convert.ToDouble(info.Length / 1000000);
// Default limit of 4 MB on web server // have to change the web.config to if // you want to allow larger uploads if (length < 4) { // set up a file stream and binary reader for the // selected file FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(stream);
// convert the file to a byte array byte[] data = reader.ReadBytes((int)size); reader.Close();
// pass the byte array (file) and file name to the web service string status = service.UploadFile(data, file, directory);
//close and clean up stream.Close(); stream.Dispose(); service.Dispose();
// this will always say OK unless an error occurs, // if an error occurs, the service returns the error message //MessageBox.Show("File Upload Status: " + status, "File Upload"); } else { // Display message if the file was too large to upload MessageBox.Show("The file selected exceeds the size limit for uploads.", "File Size"); } } catch (Exception ex) { // display an error message to the user MessageBox.Show(ex.Message.ToString(), "Upload Error"); } }
Going that route is an option as well. Now you have two options to look at
Thanks for your advice, eclipsed4utoo! Why do you think using web control to upload big files is of better performance than writing a client side application to upload? Any more descriptions? My confusion is I am refering to some other similar applications, like how Google deal with large amout of picture upload, it also has a client side picasa application.
QUOTE(eclipsed4utoo @ 24 Dec, 2008 - 09:20 AM)
I would do it from the browser. If you are going to be uploading big files, let the server to the heavy lifting. ASP.Net has an upload file control and it is very simple to implement.
protected void cmdUploadFile_Click(object sender, EventArgs e) { string strFileNameOnServer = uplTheFile.PostedFile.FileName; string strBaseLocation = @"C:\Inetpub\wwwroot\TempFolder"; // This is the location on your server where you want the files to be uploaded
if (string.IsNullOrEmpty(strFileNameOnServer)) { lblInformation.Text = "Error - a file name must be specified."; return; }
Using a client side application to call web service v.s. using web control to upload, which solution do you think is of better performance when uploading large binary files?
QUOTE(PsychoCoder @ 24 Dec, 2008 - 09:29 AM)
I'm working on an application where part of the functionality required is the ability to upload files to a web server. I created a Web Service on the web server in which I break the files into a byte array and put the file back together on the server then save it.
I haven't noticed any performance issues when doing it this way, and at times she uploads 400MB+ worth of files.
Here's the WebMethod on the web service
csharp
[WebMethod] public string UploadFile(byte[] fileBytes, string uploadFile, string uploadPath) { try { //new memory stream, pass the byte array MemoryStream memoryStream = new MemoryStream(fileBytes);
//new filestream pointing to the upload folder, pass also the file being uploaded // to name the resulting file FileStream fileStream = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(uploadPath) + uploadFile, FileMode.Create);
//use WriteTo to write the files byte array to the stream memoryStream.WriteTo(fileStream);
//close & clean up memoryStream.Close(); fileStream.Close(); fileStream.Dispose();
And the method on the Windows application looks likie this
csharp
public static void UploadImage(ref string filename, ref string directory) { try { // get the exact file name from the path String file = System.IO.Path.GetFileName(filename);
// create an instance fo the web service {REMOVED}.Core.Uploader.FileUploader service = new {REMOVED}.Core.Uploader.FileUploader();
// get the file information form the selected file FileInfo info = new FileInfo(filename);
// get the length of the file to see if it is possible // to upload it (with the standard 4 MB limit) long size = info.Length; double length = Convert.ToDouble(info.Length / 1000000);
// Default limit of 4 MB on web server // have to change the web.config to if // you want to allow larger uploads if (length < 4) { // set up a file stream and binary reader for the // selected file FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(stream);
// convert the file to a byte array byte[] data = reader.ReadBytes((int)size); reader.Close();
// pass the byte array (file) and file name to the web service string status = service.UploadFile(data, file, directory);
//close and clean up stream.Close(); stream.Dispose(); service.Dispose();
// this will always say OK unless an error occurs, // if an error occurs, the service returns the error message //MessageBox.Show("File Upload Status: " + status, "File Upload"); } else { // Display message if the file was too large to upload MessageBox.Show("The file selected exceeds the size limit for uploads.", "File Size"); } } catch (Exception ex) { // display an error message to the user MessageBox.Show(ex.Message.ToString(), "Upload Error"); } }
Going that route is an option as well. Now you have two options to look at
you know, PsychoCoder and myself have given you the code to do both. Why don't you test the performance yourself? It's not like it's going to take you 2 weeks to write the code. All you have to do is copy and paste, then do the testing yourself.
I use the web application to upload images to my webserver. I couldn't tell you the performance of larger files.