File Upload:
To use controls-FileUpload
1, how to determine whether to select the file?
Fileupload.filename-
The file name of the selected file, if the length is not greater than 0, then no file is selected
Js-f.value.length
2, how to save to the server?
Fileupload.saveas ("absolute path");
3. How to get the absolute path?
Write the relative path first-"Uploads/abc.txt"
Mapping relative paths to absolute paths-Server.MapPath ("Uploads/abc.txt");
4, now can only be uploaded into TXT file, and the name can only be ABC
How do I keep a file's original name and file type?
"uploads/" + fileupload1.filename;
5, how to prevent the problem of duplicate name coverage?
"uploads/" + request.cookies["user"]. Value + DateTime.Now.ToString ("yyyymmddhhmmssms") + fileupload1.filename;
6. How to restrict the type of selected files?
Restrict ordinary people, add properties to the control-accept= ". Jpg,.png,.jpeg,.txt"
Limit the trouble:
document.getElementById ("Button1"). onclick = function () {
var fileName = document.getElementById ("FileUpload1"). Value;
var name = Filename.substr (filename.length-4, 4);
var name1 = Filename.substr (filename.length-5, 5);
if (name! = ". jpg" && name! = ". png" && name! = ". txt" && name1! = ". jpeg")
{
Alert ("Please select the correct file!") You're looking for something, huh? ");
return false;
}
};
7, control the size of the upload file
Expansion-system default allowed maximum upload length is 4MB
Webconfig in the configuration file
<system.web>
<compilation debug= "false" targetframework= "4.0"/>
</system.web>
Baidu Search, research, C # large file upload, breakpoint continued to pass.
Attention! Do not expand too much, or many people at the same time uploading large files will cause server memory shortage!
Limit size
C # End Limit:
if (fileupload1.postedfile.contentlength> (4*1024*1024))
{
Label1.Text = "File length too long!!!" ";
Return
}
JS End limit:
var f = document.getElementById ("FileUpload1");
if (F.files[0].size > (4 * 1024 * 1024)) {
Alert ("File Too big!!! ");
return false;
}
Uploading images and watermarks:
Draw:
Canvas-How big
System.Drawing.Image img =System.Drawing.Image.FromStream (fileupload1.filecontent); Graphics g=graphics.fromimage (IMG);
What to drawstrings ="Chinese-Enterprise Singularity network 0928";
What font is used to draw font F=NewFont ("Microsoft Ya-Black", -);
Pen-color, weight, style brush B=NewSolidBrush (color.red); PointF P=NewPointF ( -, -);
Draw g.DrawString (S, F, B, p);
stringSSS ="images/"+datetime.now.tostring ("yyyymmddhhmmssms")+Fileupload1.filename; Img. Save (Server.MapPath (SSS)); Image1.imageurl= SSS;
Photo Verification production:
Front Desk: <asp:image id="Image1"runat="Server"Imageurl="yzm.aspx"/>yzm.aspx Backstage: Bitmap img=NewBitmap ( the, +); Graphics g=graphics.fromimage (IMG); Random R=NewRandom (); List<Color> CList =NewList<color>(); CList. ADD (Color.yellow); CList. ADD (Color.green); CList. ADD (Color.Blue); CList. ADD (Color.pink); CList. ADD (Color.orange); CList. ADD (Color.Black); G.fillrectangle (NewSolidBrush (Clist[r.next (0, CList. Count)]),0,0, the, +); for(inti =0; I <Ten; i++) {Color CCC= Clist[r.next (0, CList. Count)]; Pen PPP=NewPen (NewSolidBrush (CCC), R.next (1,5)); G.drawline (PPP,NewPointF (R.next (0, the), R.next (0, +)),NewPointF (R.next (0, the), R.next (0, +))); } stringall ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; strings =""; for(inti =0; I <4; i++) { intA =R.next (all. Length); S+ = all. Substring (A,1); } session["YZM"] =s; g.DrawString (s),NewFont ("Microsoft Ya-Black", -),NewSolidBrush (color.red),NewPointF (3,3)); Img. Save (Response.outputstream, System.Drawing.Imaging.ImageFormat.Png); Response.End ();
.
.
File upload, image watermark, verification code-January 6, 2017