Tuesday, December 21, 2004

For my current project the user can select a file for uploading to a network drive.  First of all, the network drive is outside the web server.  So this could give us troubles with security.  The reason for this is that there is an MSAccess application which doing the same.  And if possible we must provide a solution with the most little reprogramming in the MSAccess application.

The customer asked us to create a pilot with several possibilities.

So the first thing we did was creating just an upload component, which will save the file in a directory on the web server.  This is very easy.  Just put the HTMLInput control on your site (Remark: This is a standard HTML control). 

<input type="file" id="MyFile" runat="server" NAME="MyFile" />

In the click event you can use the FileName property of HTMLInput control.

string strFileName = MyFile.PostedFile.FileName;
strFileName = System.IO.Path.GetFileName(strFileName);


Now you can use the save method:

MyFile.PostedFile.SaveAs(Server.MapPath("./") + strFileName);

And done.

For more info see also at the MSDN article How To Upload a File to a Web Server in ASP.NET by Using Visual C# .NET.

You can use this principle also for multiple file upload:
We put also a listbox on the screen, where the selected files are visible for the user.  When pressing the upload button, the system will upload all files one by one.
Be aware : What really happens is that when choosing a file with the HTMLInput control, and we press the button to add this file to our list of files, we have a roundtrip to the server.  Also the file is already uploaded to a temporary directory.  You see this very well in following situation:

 - Take two large files (10 MB or so)
 - Select the first and press add to list
 - The systems will add this file in our list, but this takes some time
 - Select the second file and press add to list
 - The systems will also add this file in our list, but this takes some time
 - Now press the upload button, and the system is done immediately.

See later for bigger files, because there's a limitation.


Because we use an Oracle DB, we looked for another solution: Blob fields.
These fields are form the type "base64Binary", and can contains anything (textfiles, images, word documents, ...).

To save your data in a blob field, you must use streaming.  The same for reading the blobfield.

FileStream fs = new FileStream(@uploadFile, FileMode.OpenOrCreate, FileAccess.Read);
byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));

fs.Close();

Now you can write MyData in your table field.

The same way for reading :

byte[] MyData= new byte[0];

<Here you put the tablefield content in MyData.

int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);

FileStream fs = new FileStream(@"C:\temp\" + fName.Text.ToString(), FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0,ArraySize);
fs.Close();

All this is perfectly described at the MSDN article How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET (although this with using SQLServer).

Uploading larger files
Except when using the Blob fields, we have by default a limitation to upload files not bigger then 4,096 kilobytes.  When you want to upload bigger files you have to change the maxRequestLength parameter of the httpRuntime section in the web.config file.

When you want to change this for all your applications, you can modify the Machine.config (Located in the \System Root\Microsoft.NET\Framework\Version Number\CONFIG directory).  Be sure not to set this parameter too low.  This because when the file is bigger, IE will display cannot find server or DNS" error message.  You cannot use a custom error page.  While looking for this, I came across an article on developer.com which provide a solution so you could give a custom error message.  You can find the article here.

As sson as I know what solution we will implement to save the file on a netwrok drive, I will update this article.

12/21/2004 3:02:55 PM (Romance Standard Time, UTC+01:00)  #     |