6 Replies - 9019 Views - Last Post: 25 December 2008 - 09:11 AM Rate Topic: -----

#1 George2   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 14-December 08

any ideas for file upload solution

Posted 22 December 2008 - 11:42 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: any ideas for file upload solution

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: any ideas for file upload solution

Posted 23 December 2008 - 03: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
Was This Post Helpful? 0
  • +
  • -

#3 George2   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 14-December 08

Re: any ideas for file upload solution

Posted 24 December 2008 - 01:02 AM

Hi PsychoCoder,

View PostPsychoCoder, on 23 Dec, 2008 - 02:10 PM, said:

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
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: any ideas for file upload solution

Posted 24 December 2008 - 10: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
<input id="uplTheFile" type="file" runat="server" style="width: 476px; height: 26px">



Add a button to the web form..
<asp:Button id="cmdUploadFile" value="Upload" runat="server" Width="200px" Text="Upload File" Height="32px" onclick="cmdUploadFile_Click" />



and in your code-behind...
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;
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: any ideas for file upload solution

Posted 24 December 2008 - 10: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

[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

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 :)
Was This Post Helpful? 0
  • +
  • -

#6 George2   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 14-December 08

Re: any ideas for file upload solution

Posted 24 December 2008 - 10:22 PM

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.

View Posteclipsed4utoo, on 24 Dec, 2008 - 09:20 AM, said:

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
<input id="uplTheFile" type="file" runat="server" style="width: 476px; height: 26px">



Add a button to the web form..
<asp:Button id="cmdUploadFile" value="Upload" runat="server" Width="200px" Text="Upload File" Height="32px" onclick="cmdUploadFile_Click" />



and in your code-behind...
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?

View PostPsychoCoder, on 24 Dec, 2008 - 09:29 AM, said:

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

[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

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


regards,
George
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: any ideas for file upload solution

Posted 25 December 2008 - 09:11 AM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1