Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a ASP.NET Expert!

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!




any ideas for file upload solution

 
Reply to this topicStart new topic

any ideas for file upload solution

George2
22 Dec, 2008 - 10:42 PM
Post #1

D.I.C Head
**

Joined: 14 Dec, 2008
Posts: 83



Thanked: 1 times
My Contributions
Hello everyone,

My problem is,

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.

Any documents or samples for me to start with?

thanks in advance,
George

User is offlineProfile CardPM
+Quote Post


PsychoCoder
RE: Any Ideas For File Upload Solution
23 Dec, 2008 - 02:10 PM
Post #2

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,288



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Well will they be uploading the document from a web application or from a Windows application, that changes your options quite a bit
User is offlineProfile CardPM
+Quote Post

George2
RE: Any Ideas For File Upload Solution
24 Dec, 2008 - 12:02 AM
Post #3

D.I.C Head
**

Joined: 14 Dec, 2008
Posts: 83



Thanked: 1 times
My Contributions
Hi PsychoCoder,

QUOTE(PsychoCoder @ 23 Dec, 2008 - 02:10 PM) *

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.

What is your idea?

regards,
George

User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: Any Ideas For File Upload Solution
24 Dec, 2008 - 09:20 AM
Post #4

D.I.C Lover
Group Icon

Joined: 21 Mar, 2008
Posts: 1,170



Thanked: 117 times
Dream Kudos: 125
My Contributions
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.

use this HTML code
CODE

<input id="uplTheFile" type="file" runat="server" style="width: 476px; height: 26px">


Add a button to the web form..
CODE

<asp:Button id="cmdUploadFile" value="Upload" runat="server" Width="200px" Text="Upload File" Height="32px" onclick="cmdUploadFile_Click" />


and in your code-behind...
CODE

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;
    }

    if (uplTheFile.PostedFile != null)
    {
        try
        {
            uplTheFile.PostedFile.SaveAs(strBaseLocation + strFileNameOnServer);
            lblInformation.Text = "File <b>" + strFileNameOnServer + "</b> uploaded successfully";
        }
        catch (Exception ex)
        {            
            lblInformation.Text = "Error saving <b>" + strFileNameOnServer + "</b><br>.  " + ex.Message;
        }
    }
}

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Any Ideas For File Upload Solution
24 Dec, 2008 - 09:29 AM
Post #5

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,288



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
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();


//return our successful message
return "File upload complete";
}
catch (Exception ex)
{
//return the failed message
return ex.Message.ToString();
}


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 smile.gif
User is offlineProfile CardPM
+Quote Post

George2
RE: Any Ideas For File Upload Solution
24 Dec, 2008 - 09:22 PM
Post #6

D.I.C Head
**

Joined: 14 Dec, 2008
Posts: 83



Thanked: 1 times
My Contributions
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.

use this HTML code
CODE

<input id="uplTheFile" type="file" runat="server" style="width: 476px; height: 26px">


Add a button to the web form..
CODE

<asp:Button id="cmdUploadFile" value="Upload" runat="server" Width="200px" Text="Upload File" Height="32px" onclick="cmdUploadFile_Click" />


and in your code-behind...
CODE

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;
    }

    if (uplTheFile.PostedFile != null)
    {
        try
        {
            uplTheFile.PostedFile.SaveAs(strBaseLocation + strFileNameOnServer);
            lblInformation.Text = "File <b>" + strFileNameOnServer + "</b> uploaded successfully";
        }
        catch (Exception ex)
        {            
            lblInformation.Text = "Error saving <b>" + strFileNameOnServer + "</b><br>.  " + ex.Message;
        }
    }
}



regards,
George


Thanks PsychoCoder,

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();


//return our successful message
return "File upload complete";
}
catch (Exception ex)
{
//return the failed message
return ex.Message.ToString();
}


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 smile.gif


regards,
George

User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: Any Ideas For File Upload Solution
25 Dec, 2008 - 08:11 AM
Post #7

D.I.C Lover
Group Icon

Joined: 21 Mar, 2008
Posts: 1,170



Thanked: 117 times
Dream Kudos: 125
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 07:11PM

Live ASP.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

ASP.NET Tutorials

Reference Sheets

ASP.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month