Asp.net File Upload instance Summary

Source: Internet
Author: User

Based on the. NET framework class library, ASP. net encapsulates a large number of functions, making File Uploading very simple. There are three basic methods.

Method 1: Use the FileUpload Web control to upload the file to the root directory of the website.

Key code of Test. aspx:

Copy codeThe Code is as follows:
<Form id = "form1" runat = "server">
<Asp: FileUpload ID = "FileUpload1" runat = "server"/>
<Asp: Button ID = "Button1" runat = "server" Text = "Upload" OnClick = "button#click"/>
<Asp: Label ID = "Label1" runat = "server" Text = "" Style = "color: Red"> </asp: Label>
</Form>

Key code Test. aspx. cs:

Copy codeThe Code is as follows:
Protected void button#click (object sender, EventArgs e)
{
If (FileUpload1.HasFile)
{
FileUpload1.SaveAs (Server. MapPath ("~ /") + FileUpload1.FileName );
Label1.Text = "Upload successful! ";
}
}

Method 2: Use the Html control HtmlInputFile to upload the file to the root directory of the website.

Key code of Test. aspx:

Copy codeThe Code is as follows:
<Form id = "form1" runat = "server">
<Input type = "file" id = "file1" runat = "server"/>
<Asp: Button ID = "Button1" runat = "server" Text = "Upload" OnClick = "button#click"/>
<Asp: Label ID = "Label1" runat = "server" Text = "" Style = "color: Red"> </asp: Label>
</Form>

Key code Test. aspx. cs:

Copy codeThe Code is as follows:
Protected void button#click (object sender, EventArgs e)
{
If (file1.PostedFile. ContentLength> 0)
{
File1.PostedFile. SaveAs (Server. MapPath ("~ /") + Path. GetFileName (file1.PostedFile. FileName ));
Label1.Text = "Upload successful! ";
}
}

Method 3: Use the Html element <input type = "file "... />, Upload to the root directory of the website through Request. Files.

Key code of Test. aspx:

Copy codeThe Code is as follows:
<Form id = "form1" runat = "server" enctype = "multipart/form-data">
<Input type = "file" name = "file"/>
<Asp: Button ID = "Button1" runat = "server" Text = "Upload" OnClick = "button#click"/>
<Asp: Label ID = "Label1" runat = "server" Text = "" Style = "color: Red"> </asp: Label>
</Form>

Key code Test. aspx. cs:

Copy codeThe Code is as follows:
Protected void button#click (object sender, EventArgs e)
{
If (Request. Files ["file"]. ContentLength> 0)
{
Request. Files ["file"]. SaveAs (Server. MapPath ("~ /") + Path. GetFileName (Request. Files [" file "]. FileName ));
Label1.Text = "Upload successful! ";
}
}

Note two differences:

I. FileUpload. fileName obtains the client Upload File Name (without path), while file1.PostedFile. fileName and Request. files ["file"]. fileName varies in different browsers: IE8 obtains the full name (with Path) of the File Uploaded by the client, while Google, apple, and other browsers still have the file name (without path ).

II. The FileUpload control has the HasFile attribute, which is used to determine whether the user has selected the file to be uploaded. The latter two methods need to determine the ContentLength attribute of the file to be uploaded. When the user has not selected the file to be uploaded, the property value is 0.

We can see that FileUpload is more encapsulated, but it is slightly less flexible.

Example: Asp.net File Upload class (get the file suffix, save the file, and add a text watermark)

Copy codeThe Code is as follows:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Drawing;
Using System. IO;
Using System. Drawing. Imaging;

Namespace EC
{
/// <Summary>
/// Upload class
/// </Summary>
Public class UploadObj
{

Public UploadObj ()
{
//
// TODO: add the constructor logic here
//
}
/// <Summary>
/// Enumeration of File Upload types
/// </Summary>
Public enum FileType
{
Jpg, gif, bmp, png
}

# Region obtain the file suffix
/// <Summary>
/// Obtain the file suffix
/// </Summary>
/// <Param name = "filename"> file name </param>
/// <Returns> </returns>
Public static string GetFileExtends (string filename)
{
String ext = null;
If (filename. IndexOf ('.')> 0)
{
String [] fs = filename. Split ('.');
Ext = fs [fs. Length-1];
}
Return ext;
}
# Endregion

# Region check whether the file is valid
/// <Summary>
/// Check whether the uploaded file is valid
/// </Summary>
/// <Param name = "fileExtends"> file suffix </param>
/// <Returns> </returns>
Public static bool CheckFileExtends (string fileExtends)
{
Bool status = false;
FileExtends = fileExtends. ToLower ();
String [] fe = Enum. GetNames (typeof (FileType ));
For (int I = 0; I <fe. Length; I ++)
{
If (fe [I]. ToLower () = fileExtends)
{
Status = true;
Break;
}
}
Return status;
}
# Endregion

# Region save the file
/// <Summary>
/// Save the file
/// </Summary>
/// <Param name = "fpath"> full path, Server. MapPath () </param>
/// <Param name = "myFileUpload"> upload Control </param>
/// <Returns> </returns>
Public static string PhotoSave (string fpath, FileUpload myFileUpload)
{
String s = "";
String fileExtends = "";
String fileName = myFileUpload. FileName;
If (fileName! = "")
{
// Obtain the file suffix
FileExtends = EC. UploadObj. GetFileExtends (fileName );
If (! EC. UploadObj. CheckFileExtends (fileExtends ))
{
EC. MessageObject. ShowPre ("the uploaded file type is invalid ");
}
Random rd = new Random ();
S = EC. RandomObject. DateRndName (rd) + "." + fileExtends;
String file = fpath + "\" + s;
Try
{
MyFileUpload. SaveAs (file );
}
Catch (Exception ee)
{
Throw new Exception (ee. ToString ());
}
}
Return s;
}

# Endregion

# Add a text watermark to region

/// <Summary>
/// Add a text watermark
/// </Summary>
/// <Param name = "fileName"> file name path (full path) </param>
/// <Param name = "text"> file </param>
Public void AddTextToImg (string fileName, string text)
{
If (! File. Exists (fileName ))
{
Throw new FileNotFoundException ("the file does not exist ");
}
If (text = string. Empty)
{
Return;
}
// Determine whether the file type is image

System. Drawing. Image image = System. Drawing. Image. FromFile (fileName );
Bitmap bitmap = new Bitmap (image, image. Width, image. Height );
Graphics g = Graphics. FromImage (bitmap );
Float fontSize = 12.0f; // font size
Float textWidth = text. Length * fontSize; // the Length of the text.
// The following defines a rectangle area, and then draws white and black characters in the rectangle.
Float rectX = 0;
Float rectY = 0;
Float rectWidth = text. Length * (fontSize + 8 );
Float rectHeight = fontSize + 8;
// Declare the rectangular Field
RectangleF textArea = new RectangleF (rectX, rectY, rectWidth, rectHeight );
Font font = new Font ("", fontSize); // define the Font
Brush whiteBrush = new SolidBrush (Color. White); // White Brush, used to draw text
Brush blackBrush = new SolidBrush (Color. Black); // Black Brush for background painting
G. FillRectangle (blackBrush, rectX, rectY, rectWidth, rectHeight );
G. DrawString (text, font, whiteBrush, textArea );
MemoryStream MS = new MemoryStream ();
Bitmap. Save (MS, ImageFormat. Jpeg );
// Output the processed image. For demonstration convenience, I will display the image on the page
// Response. Clear ();
// Response. ContentType = "image/jpeg ";
// Response. BinaryWrite (ms. ToArray ());
G. Dispose ();
Bitmap. Dispose ();
Image. Dispose ();
}
# Endregion
}
}

Disadvantages of ASP. NET

ASP. NET processes file uploads. The biggest problem is that the memory usage is too high. Because the entire file is loaded into the memory for processing, if the user uploads too many files or uploads too many users at the same time, this will cause the server to run out of memory. This is actually one-sided, for the early ASP. NET 1.X, in order to be processed by the program, the user-uploaded content will be fully loaded into the memory, which indeed brings problems, but in ASP. NET 2.0 already exists in a temporary file on the hard disk after the user uploads more data. This is completely transparent to developers, that is, developers can process data streams as before.
The requestLengthDiskThreshold attribute is used to set the threshold value (threshold). The default value is 256. That is, when the request content exceeds kb, the hard disk is used as the cache. This threshold value is irrelevant to whether the client is uploading content, only the request sent from the client is greater than this value. Therefore, in ASP. NET 2.0, the server's memory will not be exhausted due to abnormal client requests. Another drawback is that when the request exceeds maxRequestLength (4 MB by default), ASP. NET will not process the request. This is different from ASP. NET throws an exception completely different, which is why if the user uploads a file too large, it does not see ASP. the error page specified in the. NET application (or the default one), because ASP. NET has not processed this request.


Another problem is that the processing of ASP. NET large file upload times out. In fact, you can read the web at runtime. the httpRuntime section in config and converts it to the HttpRuntimeSection object or overwrites the Page. onError () is used to check whether the HTTP Code is 400.


The Code is as follows:

Copy codeThe Code is as follows:
System. Configuration. Configuration
Config = WebConfigurationManager.
OpenWebConfiguration ("~ ");
HttpRuntimeSection section = config. GetSection
("System. web/httpRuntime") as HttpRuntimeSection;
Double maxFileSize = Math. Round
(Section. MaxRequestLength/1024.0, 1 );
String errorString = string. Format ("Make sure
Your file is under {0: 0. #} MB. ", maxFileSize );

Protected override void OnError (EventArgs e)
{
HttpContext ctx = HttpContext. Current;
Exception exception = ctx. Server. GetLastError ();

String errorString =
"
Offending URL: "+ ctx. Request. Url. ToString () +
"
Source: "+ exception. Source +
"
Message: "+ exception. Message +
"
Stack trace: "+ exception. StackTrace;

Ctx. Response. Write (errorString );

Ctx. Server. ClearError ();

Base. OnError (e );
}

Special requirements are required for file upload functions, such as progress bar prompts. ASP. NET encapsulated controls <asp: FileUpload/> are powerless.

Good Solution

Robert Bazinet suggested that the best solution is to use RIA. In most cases, it is recommended to use the Silverlight or Flash Upload Component to replace the traditional FileUpload component, this type of component not only provides a better upload experience, but also is more beautiful than the text box and button of the <input type = "file"> label on the page, the <input type = "file"> label cannot be used to add styles through CSS, but some people try to solve the problem. So far, no commercial Upload Component has used Silverlight, but here we have demonstrated a sample program for multi-File Upload using Silverlight. Of course, with Silverlight, you can easily implement multi-threaded upload and resumable upload. These are not the details that I want to discuss in detail. If you need them, you can check them yourself.

Available solutions

The use of the <input type = "file"/> label provides very limited support. We cannot implement some special requirements-or simply cannot implement them easily or directly. Therefore, every time we implement such a function, we have to go around a big bend. To avoid time-consuming detours every time you implement the same functions, various Upload components are available in the market or open-source world. The Upload components provide encapsulated functions, this makes it easier to implement the file upload function. For example, almost all Upload components provide progress prompts directly or indirectly, some provide the current percentage value, and some directly provide a set of UI; some components only provide a simple UI, while others provide a complete set of upload and deletion management interfaces. In addition, some components provide the ability to prevent malicious client uploads.

I think the best way is to read files in parts in HttpModule and maintain the page activation status, so that it will not time out, and you can also track the progress or cancel the upload, or implement it through HttpHandler, when the progress bar is displayed to the user, developers can better control the file size and possible exceptions during the upload process. These methods are used to upload components. Our options include:

Copy codeThe Code is as follows:
FileUploader. NET (MediaChase, $310 or above)
RadUpload (Telerik, $249)
NeatUpload (free of charge, compliant with LGPL Protocol)

······

NeatUpload intercepts the current HttpWorkerRequest object in the BeginRequest event of ASP. NET Pipeline, and then directly calls its ReadEntityBody and other methods to obtain the data stream passed by the client for analysis and processing. The new request is used for polling to obtain the current upload status. For more information about NeatUpload and other open-source components, see JeffreyZhao's file uploading in ASP. NET applications. Of course, he also mentioned Memba Velodoc XP Edition and swfupload, which are great!

HttpWorkerRequest implementation

Using the implicit HttpWorkerRequest, you can use its GetPreloadedEntityBody and ReadEntityBody methods to read data in blocks from the pipe created by IIS for ASP. NET to upload files. The implementation method is as follows:

Copy codeThe Code is as follows:
IServiceProvider provider = (IServiceProvider)
HttpContext. Current;
HttpWorkerRequest wr = (HttpWorkerRequest)
Provider. GetService (typeof (HttpWorkerRequest ));
Byte [] bs = wr. GetPreloadedEntityBody ();
If (! Wr. IsEntireEntityBodyIsPreloaded ())
{
Int n = 1024;
Byte [] bs2 = new byte [n];
While (wr. ReadEntityBody (bs2, n)> 0)
{
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.