A few months ago, I had a task asking me to build a page to upload a PDF files, and another page to show those uploaded files, in that time it was urgent case with no time dead line, so I had to switch from uploading those files to the SQL server database, to uploading them to the server folders.
Anyway in my opinion saving those files on the folders is not the perfect solution for many reasons such as the saving size, security reasons, naming conflict, and saving references to those files in our database in case we'll need to delete this file.
UploadFileControl.PostedFile.SaveAs(Server_
.MapPath("FileToUpload.PDF"))
Of course in this case you must give your application the privilege (permissions) to write on this folder, and obviously to read from, you can give those privileges from your server IIS.
Anyway uploading the file to the database is totally another story, first of all you need a HTML upload control, after adding the control to the page, right click on it and check on "Run as server control".
Add the parameter (encType="multipart/form-data") to the form tag of your page so your form tag will appear like this:
Don't forget to check on two things, before saving the file:
- if the user did select any file?
- if the user selected file is an allowed type document.
- and maybe if you want to control the max size of selected document to upload.
After that to save the file, you will need to store some information about this file, to get it out in the same shape as you stored it, the first thing store the data field which will hold your actual file as a [byte array], the data type is "image", also you need to save the type of the file, if its PDF or Word document file, save it as VarChar field. Those two fields could be enough to upload and to show the file to the user, but extra information like the file name, or the file length could be useful for you later.
Next you'll need to translate the file into an understandable format for your database which is here Image type, after you get this value, send it straight to the database using the regular ADO.NET code.
And now how to do that, you can only use the following ready to use code:
Byte[] File2Upload = new byte[UploadFileControl.PostedFile.InputStream.Length];
String TypeFile;
To get the extension:
extension=System.IO.Path.GetExtension(UploadFileControl_
.PostedFile.FileName.ToLower());
switch (extension){
case ".gif":
TypeFile = "image/gif";
break;
case ".jpg":
TypeFile = "image/jpeg";
break;
case ".png":
TypeFile = "image/png";
break;
case ".pdf":
TypeFile = "application/pdf";
break;
case ".txt":
TypeFile = "text/plain";
break;
default:
lblWarning.Text = "Not a Valid file format";
return;
break;}
UploadFileControl.PostedFile.InputStream.Read(File2Upload, 0, File2Upload.Length);
And now you can store File2Upload into the data field and the TypeFile into the type field, using regular ADO.NET code.
After saving the file, you need to show the file to the user, to do that create a new page lets name it DownloadFile.aspx, in the code behind, use the following procedure which take the Data as Byte array, the file type as string, the length as integer, and the file name as string, retrieve these information from the database, and call the final,,
Public Sub DeliverFile(ByVal Data() As Byte, ByVal Type As String,_
ByVal Length As Integer, Optional ByVal DownloadFileName As String = "")
Response.Clear()
Response.ContentType = Type
Response.AddHeader("content-disposition", "attachment; filename=" & DownloadFileName)
Response.Charset = ""
esponse.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(Data)
Response.End()
End Sub
In the view page just insert a link to the DownloadFile.aspx and send the File Id for example using the QueryString,
lblPdf.Text = < href="'DownloadPdf.aspx?Id=">" + PdfNameHere ++"< / a >
Last thing, when the user selects a file with a big size, the application sometimes fail to upload the file, all what happens is the page just tells you: "Internet Explorer cannot display the webpage"
In this case just add the following parameter in the
The problem occur when the uploading process cross the timeout allowed by the application.
<httpRuntime executionTimeout="110" maxRequestLength="10096"/>
I think that’s it for now..
Have a nice coding!!
Osama Asa'ad