Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [134]

By Root 5486 0
!= null)

{

// Save the uploaded file to the specified path

var fileName = Path.GetFileName(FileUpload1.Value);

savePath += fileName;

FileUpload1.PostedFile.SaveAs(savePath);

// Notify the user of the name the file was saved under.

UploadStatusLabel.InnerText = "File saved as: " + savePath;

}

else

{

// Notify the user that a file was not uploaded.

UploadStatusLabel.InnerText = "No file specified.";

}

}

File Upload

Select a file to upload:


File to upload



value="Upload" onserverclick="UploadButton_Click" />


You can also use the InputStream property of the HttpPostedFile object to read the posted data before persisting or processing. The HttpInputFile control also allows you to restrict the file types that can be uploaded to the server. You do this by setting the Accept property with a comma-separated list of MIME types.

Caution

When you use the SaveAs method, you should pay attention to specify the full path to the output file. If a relative path is provided, ASP.NET attempts to place the file in the system directory. This practice can result in an “access denied” error. Furthermore, make sure to provide write permission for the account used by ASP.NET for the directory where you want to store the file.

ASP.NET exercises some control of the amount of data being uploaded. The maxRequestLength attribute in the section of the configuration file sets the maximum allowable file size. An error is generated in the browser when the file exceeds the specified size—4 MB by default. Uploading large files might also generate another run-time error as a result of an excessive consumption of system memory. Finally, in a hosting scenario if you still experience problems regardless of the settings in your configuration, check out the maximum upload size on your Web server.

The HtmlImage Control


The HtmlImage class is the ASP.NET counterpart of the tag. You can use it to configure on the server the display of an image. Possible parameters you can set are the size of the image, the border, and the alternate text. An instance of HtmlImage is created only when the runat attribute is added to the tag. If you simply need to display an image within a page, and the image is not dynamically determined or configured, there is no need to resort to the HtmlImage control, which would add unnecessary overhead to the page.

The following code snippet shows how to configure a server-side tag called to display an image whose name is determined based on run-time conditions:

theImg.Width = 100;

theImg.Height = 100;

theImg.Src = GetImageUrl(Request); // assume GetImageUrl is a method of yours

The HtmlImage control should be used to programmatically manipulate the image to change the source file, the width and height, or the alignment of the image relative to other page elements. The majority of properties of the HtmlImage control are implemented as strings, including Src—the URL of the image—and Align. Feasible values of Align are only a small set of words such as left, right, top, and so forth. These words would have been more appropriately grouped in a custom enumerated type, thus providing for a strongly typed programming model. If you think so, too, you just got the gist of the difference between HTML and Web server controls! HTML controls just mirror HTML tags; Web controls attempt to provide a more consistent and effective programming interface by exploiting the characteristics of the .NET Framework.

Literal Controls

Literal controls are a special type of server control that ASP.NET creates and uses whenever it encounters plain text that doesn’t require server-side processing. In general, everything that appears in the context of an ASP.NET page is treated

Return Main Page Previous Page Next Page

®Online Book Reader