Wednesday, November 9, 2011

Downloading and Uploading Files


ASP.NET File Download

Downloading a file in ASP.NET is straightforward and does not require you to copy the file byte-by-byte to the response page, although, as shown in Listing 7, byte-by-byte transfer is still possible if you require the contents of the file to be changed at run-time. Not having to copy the file byte-by-byte will eliminate the casting problems we encountered in the JLCA Conversion of File Download section. You must still set the http content type to indicate the file is for downloading, but instead of transferring the file byte-by-byte, you can simply call theHttpResponse.WriteFile() method to transfer the file as shown in Listing 8.
Listing 8. Downloading a file in ASP.NET
<%
try
{
System.String filename = "myFile.txt";

// set the http content type to "APPLICATION/OCTET-STREAM
Response.ContentType = "APPLICATION/OCTET-STREAM";

// initialize the http content-disposition header to
// indicate a file attachment with the default filename
// "myFile.txt"
System.String disHeader = "Attachment; Filename=\"" + filename +
"\"";
Response.AppendHeader("Content-Disposition", disHeader);

// transfer the file byte-by-byte to the response object
System.IO.FileInfo fileToDownload = new
System.IO.FileInfo("C:\\downloadJSP\\DownloadConv\\myFile.txt");
Response.Flush();
Response.WriteFile(fileToDownload.FullName);}
catch (System.Exception e)
// file IO errors
{
SupportClass.WriteStackTrace(e, Console.Error);
}
%>

Notice that in Listing 8 as in Listing 1 there are still three basic steps to downloading a file. First, the http content type is set to APPLICATION/OCTET-STREAM. Second, the Content-Disposition header is set to indicate an attachment and the default file name. However, unlike Listing 1 in the third step, the file is not sent byte-by-byte, but instead a single line sends the file contents.

ASP.NET File Upload

Uploading a file in ASP.NET is very straightforward. You have to add the File Field html server control to your Web form. Once you have added the File Field control to your HTML form you need to make sure that the form has the method="post" and enctype="multipart/form-data" attributes in order to be able to access the posted file. Listing 9 shows a sample form that accepts a single file from the user and a button to submit the form.
Listing 9. A form for uploading a file
<form id="FileUploadForm" method="post"
enctype="multipart/form-data" action="afterUpload.aspx">
<P><INPUT type="file" id="UploadedFile" runat="server"></P>
<P><INPUT type="submit" value="Submit"></P>
</form>

Once the user has added the file to the form and clicked the Submit button, you can access the posted file through the HttpRequest object. All uploaded files for the current request are stored in the HttpRequest.Files property. The Files property is an HttpFileCollection object that acts similarly to a Hashtable: a String is used as a key and the value returned is an HttpPostedFile object. The HttpPostedFile class provides similar functionality to that provided by the FormFile class in Struts. This functionality is provided through various properties in theHttpPostedFile class such as the FileName property, which returns the uploaded file's name, including directories, on the client's computer; the InputStream property, which returns a Streamobject containing the file data; and the ContentLength property which returns the length in bytes of the uploaded file. Unlike the FormFile class, the HttpPostedFile class also contains aSaveAs() method which stores the file on your server with the given file name. Listing 10 contains an example of how to use the HttpPostedFile class to save an uploaded file to disk using the original file name. Because this code requires access to the HttpRequest object containing information about the form, this code will be placed in the Page_Load() method of the next ASP.NET page or any other code that will be executed after the form has been submitted, but before the next ASP.NET page is displayed. For example, the code in Listing 10 could be placed in the Page_Load() method of the afterUpload.aspx page, which is referenced by the action attribute in Listing 9.
Listing 10. Saving an uploaded file to disk
HttpFileCollection allFiles = Request.Files;
HttpPostedFile uploadedFile = allFiles["UploadedFile"];
FileInfo uploadedFileInfo = new
FileInfo(uploadedFile.FileName);
uploadedFile.SaveAs("C:\\UploadedFiles\\" +
uploadedFileInfo.Name);
userMessage = "The file has been svaed with the filename: " +
uploadedFileInfo.Name;

Summary

The basic process for a file download (from server to client) can be the same for both JSP and ASP.NET. In fact, the JLCA will properly convert your JSP code with a few minor changes. However, the process for uploading a file may be totally different, especially if you are using Struts on the JSP side. ASP.NET has a built-in control specifically for uploading files that replaces the Struts functionality. In other words, with a file upload, you have a case where replacing JSP code with a built in ASP.NET feature is faster, easier and better than trying to make a conversion work properly.