Personal information registration is often used in web development, and personal information usually requires its own avatar or photo. Today's main introduction to use the Fileupload+img control upload photos.
Introduction to FileUpload Control usage
The PostedFile property of the FileUpload control primarily gets some basic information about the uploaded file.
. ContentLength
Gets the size of the uploaded file. The return value is of type int, in bytes.
Use
1, to determine the size of uploaded files
if (this. FileUpload1.PostedFile.ContentLength <= 4000000)//4m
{
Response.Write ("<script>alert" (' +fileupload1.postedfile.contentlength + ') </script> ");
}
Else
{
Response.Write ("<script>alert" is too big to upload!) ') </script> ");
}
Where FileUpload1 is the id,this of the upload control. FileUpload1.PostedFile.ContentLength is the size of the uploaded file
2, modify the default value of ASP.net upload
Asp. NET default maximum upload value is 4M, by modifying web.config, you can upload large files:
Copy Code code as follows:
<system.web>
executiontimeout= "3600"
/>
</system.web>
. ContentType
Gets the MIME content type of the uploaded file. PostedFile.ContentType This attribute is often used to determine the MIME type of the uploaded file, which we typically reject for certain types of uploads, such as uploading only image files, or specifying a fixed picture format.
In net, do upload file to judge file format writing summary:
Copy Code code as follows:
". *" = "Application/octet-stream"
". 001" = "application/x-001"
"." = "application/x-301"
". 323" = "text/h323"
". 906" = "application/x-906"
". 907" = "drawing/907"
". A11" = "Application/x-a11"
". ACP" = "AUDIO/X-MEI-AAC"
". Ai" = "Application/postscript"
". AIF" = "Audio/aiff"
". aifc" = "Audio/aiff"
". Aiff" = "Audio/aiff"
". Anv" = "Application/x-anv"
". Asa" = "Text/asa"
". asf" = "video/x-ms-asf"
". asp" = "text/asp"
". asx" = "video/x-ms-asf"
". Au" = "audio/basic"
". avi" = "Video/avi"
". Awf" = "Application/vnd.adobe.workflow"
". Biz" = "Text/xml"
". bmp" = "application/x-bmp"
". Bot" = "Application/x-bot"
". c4t" = "application/x-c4t"
". C90" = "APPLICATION/X-C90"
". Cal" = "Application/x-cals"
". Cat" = "Application/vnd.ms-pki.seccat"
". CDF" = "APPLICATION/X-NETCDF"
". Cdr" = "APPLICATION/X-CDR"
". Cel" = "Application/x-cel"
". cer" = "Application/x-x509-ca-cert"
". Cg4" = "Application/x-g4"
". CGM" = "APPLICATION/X-CGM"
". cit" = "application/x-cit"
". Class" = "java/*"
". CML" = "Text/xml"
". cmp" = "application/x-cmp"
". CMX" = "APPLICATION/X-CMX"
". Cot" = "Application/x-cot"
". CRL" = "APPLICATION/PKIX-CRL"
". Crt" = "Application/x-x509-ca-cert"
". CSI" = "APPLICATION/X-CSI"
". css" = "text/css"
". Cut" = "application/x-cut"
". dbf" = "application/x-dbf"
". dbm" = "application/x-dbm"
". dbx" = "application/x-dbx"
". DCD" = "Text/xml"
". Dcx" = "APPLICATION/X-DCX"
". Der" = "Application/x-x509-ca-cert"
". DGN" = "APPLICATION/X-DGN"
". Dib" = "Application/x-dib"
". dll" = "Application/x-msdownload"
". doc" = "Application/msword"
". Dot" = "Application/msword"
". Drw" = "APPLICATION/X-DRW"
". DTD" = "Text/xml"
". DWF" = "MODEL/VND.DWF"
". DWF" = "APPLICATION/X-DWF"
". dwg" = "APPLICATION/X-DWG"
". Dxb" = "APPLICATION/X-DXB"
". DXF" = "application/x-dxf"
". Edn" = "Application/vnd.adobe.edn"
". emf" = "application/x-emf"
". eml" = "message/rfc822"
". Ent" = "Text/xml"
". Epi" = "Application/x-epi"
". EPs" = "Application/x-ps"
". EPs" = "Application/postscript"
". Etd" = "APPLICATION/X-EBX"
". exe" = "Application/x-msdownload"
". Fax" = "Image/fax"
". Fdf" = "APPLICATION/VND.FDF"
". Fif" = "application/fractals"
". Fo" = "text/xml"
". frm" = "application/x-frm"
". g4" = "Application/x-g4"
". GBR" = "APPLICATION/X-GBR"
". gcd" = "APPLICATION/X-GCD"
". gif" = "image/gif"
". Gl2" = "APPLICATION/X-GL2"
". Gp4" = "APPLICATION/X-GP4"
". Hgl" = "Application/x-hgl"
". HMR" = "APPLICATION/X-HMR"
". Hpg" = "APPLICATION/X-HPGL"
". HPL" = "APPLICATION/X-HPL"
". hqx" = "application/mac-binhex40"
". Hrf" = "APPLICATION/X-HRF"
". HTA" = "Application/hta"
". HTC" = "Text/x-component"
". htm" = "text/html"
". html" = "text/html"
". Htt" = "text/webviewhtml"
". htx" = "text/html"
". ICB" = "APPLICATION/X-ICB"
". ico" = "Image/x-icon"
". ico" = "Application/x-ico"
". Iff" = "Application/x-iff"
". Ig4" = "Application/x-g4"
". IGs" = "Application/x-igs"
". III" = "Application/x-iphone"
". img" = "application/x-img"
". Ins" = "Application/x-internet-signup"
". ISP" = "Application/x-internet-signup"
". IVF "=" VIDEO/X-IVF "
". Java" = "java/*"
". Jfif" = "Image/jpeg"
". Jpe" = "Image/jpeg"
". Jpe" = "Application/x-jpe"
". jpeg" = "image/jpeg"
". jpg" = "application/x-jpg"
". js" = "Application/x-javascript"
". jsp" = "text/html"
". La1" = "Audio/x-liquid-file"
". Lar" = "Application/x-laplayer-reg"
". Latex" = "Application/x-latex"
". LAVs" = "Audio/x-liquid-secure"
". LBM" = "APPLICATION/X-LBM"
". Lmsff" = "Audio/x-la-lms"
". ls" = "application/x-javascript"
". Ltr" = "application/x-ltr"
. FileName
Gets the full path and name of the uploaded file. This path is the path where the file is modified in the client file. This property is usually used to get the name of the uploaded file and save it.
. Fileupload1.saveas
Saves the uploaded file to the file under the specified path. The Server.MapPath () method is usually used to save the file to the server-specified file. (Gets the physical file path for the Web server to specify the virtual path)
Picture Upload Instance
Fileupload+image control implementation upload the specified picture size, format, save to the specified path file.
. axps
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Fileup.aspx.cs" inherits= "Testcookie.fileup"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:image id= "Image1" runat= "Server" height= "180px" width= "191px" imageurl= "~/photo/untitled.png"/>
<asp:fileupload id= "FileUpload1" runat= "Server"/>
<asp:button id= "Btnupload" runat= "Server" text= "button" onclick= "Btnupload_click<span style=" font-family: Arial, Helvetica, Sans-serif ">"/></span>
<asp:button id= "Btndelete" runat= "Server" text= "button" onclick= "Btndelete_click"/>
</div>
</form>
</body>
. Axps.cs
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Namespace TestCookie
{
public partial class Fileup:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void Btnupload_click (object sender, EventArgs e)
{
int length= this. Fileupload1.postedfile.contentlength;//<span style= "COLOR: #ff0000" > Get picture Size, in bytes </SPAN>
if (length<4000)
{
Response.Write (' <script language= ' JavaScript ' >alert (' Your chosen picture is too big! ');</script> ");
}
else{
String Type=this. Fileupload1.postedfile.contenttype;//<span style= "COLOR: #ff0000" > Get uploaded file type </SPAN>
String Fullfilename = this. Fileupload1.postedfile.filename;//<span style= "COLOR: #ff0000" > Get uploaded files on the client path and name </SPAN>
String filename = fullfilename. Substring (Fullfilename. LastIndexOf ("\") +1);
String extensions = filename. Substring (filename. LastIndexOf (".") +1)//<span style= "COLOR: #ff0000" > Get file name extension </SPAN>
String name=datetime.now.tostring ("Yyyymmddhhmmss");
if (type = = "Image/jpeg" | | | type = = "Image/png")//Restricted upload format
{
This. Fileupload1.saveas (Server.MapPath ("Photo") + "\" + name + "." + Extensions)//<span style= "COLOR: #ff0000" > Save the picture at the current time, avoid uploading the image name duplicate </SPAN>
<span style= "COLOR: #ff0000" >//set upload picture size to original picture size </SPAN>
System.Drawing.Image img = System.Drawing.Image.FromFile (this. Server.MapPath ("Photo") + "\" + name + "." + extensions);//Get Picture file
int width = img. Width;
This. Image1.width = width;//This is wide set the width of the image control to picture width
int height = img. Height;
This. Image1.height = height;//This is high, set the height of the image control to the height of the picture
This. Image1.imageurl = "photo/" + name + "." + Extensions;
}
Else
{
Response.Write (' <script language= ' JavaScript ' >alert (' Your chosen picture is wrong! ');</script> ");
}
}
}
protected void Btndelete_click (object sender, EventArgs e)
{
<span style= "COLOR: #ff0000" >//Delete pictures </SPAN>
if (System.IO.File.Exists (Server.MapPath ("Photo") + "\\20130808084433.PNG")
{
Try
{
System.IO.File.Delete (Server.MapPath ("Photo") + "\\20130808084433.PNG");
}
Catch
{
throw new Exception ("File deletion failed!") ");
}
}
}
}
}
Effect
Before the file is uploaded
After the file is uploaded