Asp.net large file upload Method

Source: Internet
Author: User

Asp tutorial. net large file upload Method

Asp.net tutorial uploading large files is an incomplete and flawed field. I believe it will be improved and developed soon. If you have already solved this problem, it means you are in a good company, otherwise, you can consider using a third-party product. We can find many different ways to solve the File Upload problem. The challenge is to find out the advantages and disadvantages of different practices and then find a solution suitable for your project, this is not just about file upload!

 

<Asp: image id = "imglogo" runat = "server"/> <br/>
<Asp: label id = "labmsg" runat = "server" forecolor = "# cc0000"> </asp: label> <br/>
<Asp: fileupload id = "uploadlogo" runat = "server" width = "60%" height = "22px"/> & nbsp;
<Asp: button id = "btnupload" runat = "server" text = "Upload" onclick = "btnupload_click"/>

Aspx. cs page

// Upload button

Protected void btnupload_click (object sender, eventargs e)
{
Uploadfile uploadfileobj = new uploadfile (); // instantiated File Upload class
Uploadfileobj. maxfilesize = 100; // sets the maximum length of the uploaded file, in k
Uploadfileobj. filetype = "jpg | jpeg | gif | png"; // you can specify the file type that can be uploaded.

String uploadpath = server. mappath ("~ /Uploadfiles/other/"); // sets the full path of the upload directory
Uploadfileobj. uploadfilego (uploadpath, uploadlogo); // File Upload
Labmsg. text = uploadfileobj. uploadinfo; // Upload message prompt
If (uploadfileobj. uploadstate = true)
{
Imglogo. imageurl = "~ /Uploadfiles/other/"+ uploadfileobj. newfilename; // display the image
}

}

Uploadfile. cs File Upload class

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.html controls;
Using system. io;

Namespace common
{
/// <Summary> File Upload class </summary>
Public class uploadfile
{

# Region Field

Private string _ uploadinfo; // The returned information of file upload.
Private bool _ uploadstate; // The returned status of file upload.
Private string _ filetype; // The type of the file to be uploaded.
Private int _ filesize; // the size of the uploaded file, in the unit of B
Private int _ maxfilesize; // maximum size of the uploaded file, in B
Private string _ newfilename; // name of the uploaded file.

# Endregion

 

/// <Summary> initial File Upload class (default) </summary>
Public uploadfile ()
{
_ Uploadinfo = "none ";
_ Uploadstate = false;
_ Filetype = "*";
_ Maxfilesize = 1024000; // 1000 k is 1024 * B, in B
_ Newfilename = "";
}

# Region attributes
/// <Summary> returned information for file upload </summary>
Public string uploadinfo
{
Set {_ uploadinfo = value ;}
Get {return _ uploadinfo ;}
}

/// <Summary> the returned status of the object upload. The value true indicates that the object is successfully uploaded. The value false indicates that the object fails to be uploaded. </summary>
Public bool uploadstate
{
Set {_ uploadstate = value ;}
Get {return _ uploadstate ;}
}

/// <Summary> specifies the type of the file to be uploaded. * by default, it indicates any type or custom type, such as "jpg | gif | bmp" </summary>
Public string filetype
{
Set {_ filetype = value ;}
Get {return _ filetype ;}
}

/// <Summary> size of the uploaded file, in k </summary>
Public int filesize
{
Get {return _ filesize/1024 ;}
}

/// <Summary> maximum size of the uploaded file, in k </summary>
Public int maxfilesize
{
Set {_ maxfilesize = value * 1024 ;}
Get {return _ maxfilesize/1024 ;}
}

/// <Summary> name of the uploaded file </summary>
Public string newfilename
{
Set {_ newfilename = value ;}
Get {return _ newfilename ;}
}

# Endregion


# Region upload main program
/// <Summary> upload a local file to the server </summary>
/// <Param name = "strsavedir"> physical path saved on the server. </Param>
/// <Param name = "fileuploadctrlid"> specifies the object to be uploaded. The fileupload control is used here. </param>
/// <Param> If the second parameter is an html input (file) control, you can change it to htmlinputfile htmctrlobjuploadfile. </param>
/// <Returns> </returns>
Public void uploadfilego (string strsavedir, fileupload fileuploadctrlid)
{
Int intfileextpoint = fileuploadctrlid. postedfile. filename. lastindexof ("."); // the location of the last.
String strfileextname = fileuploadctrlid. postedfile. filename. substring (intfileextpoint + 1). tolower (); // obtain the suffix of the uploaded file.

_ Filesize = fileuploadctrlid. postedfile. contentlength; // the size of the uploaded file. byte

If (_ filesize = 0) // determine whether a file needs to be uploaded or whether the selected file is 0 bytes.
{
_ Uploadinfo = "the file to be uploaded is not selected or the selected file size is 0 bytes ";
_ Uploadstate = false;
Return; // return the File Upload Status and information.
}

If (_ filesize> _ maxfilesize) // limit the size of the file to be uploaded (bytes ).
{
_ Uploadinfo = "the size of the uploaded file exceeds the limit (" + (_ maxfilesize/1024). tostring () + "k )";
_ Uploadstate = false;
Return; // return the File Upload Status and information.
}

If (_ filetype! = "*")
{
If (_ filetype. tolower (). indexof (strfileextname. tolower (). trim () =-1) // determines whether the file type to be uploaded is within the permitted range.
{
_ Uploadinfo = "types of files that cannot be uploaded (allowed types: |" + _ filetype + ")";
_ Uploadstate = false;
Return; // return the File Upload Status and information
}
}

If (_ newfilename = "")
{
Datetime dtenow = datetime. now; // defines the date object, which is the name of the uploaded file.
_ Newfilename = dtenow. year. tostring () + dtenow. month. tostring () + dtenow. day. tostring () + getrandomstr (8); // randomly name the uploaded file, date + random number.
_ Newfilename = _ newfilename + "." + strfileextname; // file name containing the extension
}
Fileuploadctrlid. postedfile. saveas (this. getsavedirectory (strsavedir) + _ newfilename); // Save the uploaded file to the specified physical path with a new file name.
_ Uploadinfo = "File Uploaded successfully"; // return the physical path of the uploaded Server File.
_ Uploadstate = true;

}
# Endregion

 

/// <Summary> obtain the random number of specified digits </summary>
/// <Param name = "rndnumcount"> Number of random digits. </Param>
/// <Returns> </returns>
Private string getrandomstr (int rndnumcount)
{
String randomstr;
Randomstr = "";
Random rnd = new random ();
For (int I = 1; I <= rndnumcount; I ++)
{
Randomstr + = rnd. next (0, 9). tostring ();
}
Return randomstr;
}

 


/// <Summary> obtain the Upload File storage directory </summary>
/// <Param name = "directorypath"> physical path for storing files. </Param>
/// <Returns> returns the directory where the file is stored. </Returns>
Public string getsavedirectory (string directorypath)
{
If (! Directory. exists (directorypath) // checks whether the current directory exists.
{
Directory. createdirectory (directorypath); // create a directory for storing uploaded files.
}
Return directorypath;
}

}

 

# Region attachment: Modify the upload size Configuration
/*
Which of the following statements must be modified?
In the c: windowsmicrosoft. netframeworkv1.1.4322config directory,
Find the maxrequestlength = "4096" File"
Increase the value, for example, 102400.
The unit of this parameter should be kb.

The above method is to modify the global. If you need to modify a project, it is to modify the web. config file in the project.

Add between <system. web> </system. web>,
<Httpruntime usefullyqualifiedredirecturl = "true" maxrequestlength = "21000" executiontimeout = "300"/>
Where,
Maxrequestlength: sets the maximum value of the uploaded file. Unit: kb. (The default value is 4096kb, that is, 4 MB)
Executiontimeout: sets the timeout time in seconds. (90 seconds by default)
*/
# Endregion
}

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 the asp.net application. Of course, he also talked about memba velodoc xp edition and swfupload.

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.