How to Upload File in APS.NET and Save it in the Database

Custom Search
When collecting binary data, oftentimes this data is supplied by an end
user. To capture this information, the user needs to be able to upload a
file from their computer to the web server. The uploaded data then needs to
be integrated with the data model, which may mean saving the file to the web
server's file system and adding a path to the file in the database, or
writing the binary contents directly into the database. In this step we'll
look at how to allow a user to upload files from their computer to the
server. In the next tutorial we'll turn our attention to integrating the
uploaded file with data model.

Step 1:


Sub LoadFile()

If FileUpload1.HasFile = False Then
Label1.Text = "Please first select a file to upload..."
Else
Label1.Text = String.Format("Uploaded file: {0}
" & "File
size (in bytes): {1:N0}
" & "Content-type: {2}", FileUpload1.FileName,
FileUpload1.FileBytes.Length, FileUpload1.PostedFile.ContentType)
Dim filePath As String = Server.MapPath("~/Brochures/" &
FileUpload1.FileName)

' Save this filePath in the DataTable




FileUpload1.SaveAs(filePath)
End If
End Sub



Subtleties with Saving Uploaded Files to the File System

There are several subtleties that must be addressed when saving uploading
files to the web server's file system. First, there's the issue of security..
To save a file to the file system, the security context under which the
ASP.NET page is executing must have Write permissions. The
ASP.NETDevelopment Web Server runs under the context of your current
user account.
If you are using Microsoft's Internet Information Services (IIS) as the web
server, the security context depends on the version of IIS and its
configuration.

Another challenge of saving files to the file system revolves around naming
the files. Currently, our page saves all of the uploaded files to the
~/Brochures directory using the same name as the file on the client's
computer. If User A uploads a brochure with the name Brochure.pdf, the file
will be saved as ~/Brochure/Brochure.pdf. But what if sometime later User B
uploads a different brochure file that happens to have the same filename
(Brochure.pdf)? With the code we have now, User A's file will be overwritten
with User B's upload.

There are a number of techniques for resolving file name conflicts. One
option is to prohibit uploading a file if there already exists one with the
same name. With this approach, when User B attempts to upload a file named
Brochure.pdf, the system would not save their file and instead display a
message informing User B to rename the file and try again. Another approach
is to save the file using a unique file name, which could be a globally
unique identifier (GUID) or the value from the corresponding database
record's primary key column(s) (assuming that the upload is associated with
a particular row in the data model). In the next tutorial we'll explore
these options in more detail.

*
Challenges Involved with Very Large Amounts of Binary Data*

These tutorials assume that the binary data captured is modest in size.
Working with very large amounts of binary data â€" files that are several
megabytes or larger â€" introduces new challenges that are beyond the scope of
these tutorials. For example, by default ASP.NET will reject uploads of more
than 4 MB, although this can be configured through the element
in Web.config. IIS imposes its own file upload size limitations, too. See
IIS Upload File Size for more information. Furthermore, the time taken to
upload large files might exceed the default 110 seconds ASP.NET will wait
for a request. There are also memory and performance issues that arise when
working with large files.

The FileUpload control is impractical for large file uploads. As the file's
contents are being posted to the server, the end user must patiently wait
without any confirmation that their upload is progressing. This is not so
much an issue when dealing with smaller files that can be uploaded in a few
seconds, but can be an issue when dealing with larger files that may take
minutes to upload. There are a variety of third-party file upload controls
that are better suited for handling large uploads and many of these vendors
provide progress indicators and ActiveX upload managers that present a much
more polished user experience.

 del.icio.us  Stumbleupon  Technorati  Digg 

 

What did you think of this article?




Trackbacks
  • Trackbacks are closed for this entry.
Comments
Page: 1 of 1
Page: 1 of 1
Leave a comment

Submitted comments will be subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.