File upload, image watermark, verification code-January 6, 2017

Source: Internet
Author: User
Tags server memory

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

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.