b/S----file upload, image watermark, verification code

Source: Internet
Author: User

File Upload:

//Change the relative path to an absolute path.
string absolutepath = Server.MapPath (relativepath);

FileUpload CONTROLS:
Properties:
FileName : File name
Hasfile:bool Whether the file is selected
filebytes: Binary data to upload a file
Method:
SaveAs (String absolute path): Upload, Save As.

one, upload to the hard disk folder
(i) Leaflets documents
The first step: Prepare the file and path:
Remove the file name from the client before
string fileName = Fileupload1.filename;

Prevent file names
filename = DateTime.Now.ToString ("yyyymmddhhmmsss") + filename;

Convert relative path to absolute path
String path = Server.MapPath ("uploads/" + fileName);

Step two: Perform the upload:
Upload
Fileupload1.saveas (path); parameter must root path

Attention:
1. How do I prevent file names?
2. How to prevent different users from the same point of time traditional one file name?
Each client login has its own ssesion space, plus session["user name" (or "key name") to differentiate.
(ii) Transfer of multiple documents:
Idea: Traverse all the FileUpload controls in the form and upload if you select a file

int index = 0;
foreach (Control ctrl in Form1. Controls)
{
If (Ctrl is FileUpload)
{
index++;
Get each upload control
FileUpload upload = Ctrl as FileUpload;
The upload control has the file selected.
if (upload. HasFile)
{
Make file path out
String path = Server.MapPath ("uploads/" + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + index. ToString ("xx") + Upload. FileName);

Upload
Upload. SaveAs (path);
}
}
}

second, upload to the database image field:
(i) upload to the database
1. Do the operation code of the database. DA Data
The image field corresponds to the byte[] type in the program.

2. Do the code on the interface.
A. Remove the value of the interface
Fileupload1.filebytes-binary data for uploading files.
B. Send to the database

(b) find it out from the database and show it

Law one: Cons: Garbage files are generated
Generate a JPG on the server and assign the path of this JPG to the image control

Remove the data from the database
Pictureda da = new Pictureda ();
PictureData Data=da. Select (Convert.ToInt32 (TextBox1.Text));

Flow
String temp = Server.MapPath ("imag/temp.jpg");
FileStream fs = new FileStream (temp, filemode.create);
Fs. Write (data. Fill, 0, data. Fill.length);
Fs. Close ();
Show
Image1.imageurl = "Imag/temp.jpg"

Law II: Make a separate page to display the binary data of the picture. Assign this page to the image control.
New page:
string ids = request["id"]. ToString ();

PictureData data = new Pictureda (). Select (Convert.ToInt32 (IDs));
if (data!=null)
{
Byte[] Buff=data. Fill;
Response.OutputStream.Write (Buff,0,buff. Length);
Response.End ();
}

buttons to display pictures:
Image1.imageurl = "default2.aspx?id=" +textbox1.text;



uploading images and watermarks:
//One, from the upload data, converted into a picture object
Stream s = fileupload1.filecontent;
System.Drawing.Image img = System.Drawing.Image.FromStream (s);
//Second, the picture object to draw the watermark
//1. Making Pens
SolidBrush brush = new SolidBrush (color.yellow);
//2. Making Fonts
Font font = new Font ("Microsoft Jas Black", 18);
//3. Finding the drawing area
Graphics g = Graphics.fromimage (IMG);
//4. Determining where to start the drawing
float x = 0, y = 0;
SizeF size = g.measurestring ("http:\\www.hao123.com", font);
x = img. Width-size. Width;
y = img. Height-size. Height;
//5. Start Drawing
g.DrawString ("http:\\www.hao123.com", font, brush, x, y);

//Three, Picture object Save as hard drive up
string filename = Fileupload1.filename;
String path = Server.MapPath ("imag/" +datetime.now.tostring ("yyyyMMdd") + filename);
Img. Save (path);

Verification Code: Note using System.Drawing;
Using System.IO;

Verification code Page

public partial class yzm:system.web.ui.page{protected void Page_Load (object sender, EventArgs e) {//Create a picture        Bitmap img = new Bitmap (60, 30);        Generates a random number of string str = "abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";        Random random = new random ();        String code = ""; for (int i = 0; i < 4; i++) {int start = random. Next (str.            Length); Code + = str.        Substring (start, 1);        }//Put the random number on the picture//build pen SolidBrush brush = new SolidBrush (color.white);        Font font = new Font ("Song Body", 15);        Find the drawing area Graphics g = Graphics.fromimage (IMG);        Find the location//a. Apply White g.fillrectangle (brush, 0, 0, 60, 30) to the background of the picture; B. Change the brushes to a brush color.        Color = color.red;        Start Drawing g.drawstring (code,font,brush,0,0); Save the image to the output stream to IMG.    Save (Response.outputstream,system.drawing.imaging.imageformat.jpeg); }}

The main page does not write code, but imageurl= "yzm.aspx" Call Verification code page,onclick= "Changpic ()" JS code every Click to change the diagram.

<body>    <form id= "Form1" runat= "Server" >    <div>            Verification Code: <asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>        <asp:image id= "Image1" runat= "Server" imageurl= "yzm.aspx"   Onclick= "Changpic ()"/>        <br/>        <asp:button id= "Button1" runat= "Server" text= "button"/>        </div>    </form></body>

Javascript:

<script language= "javascript" >        function changpic ()        {            var img = document.getElementById ("Image1 ");            Img.setattribute ("src", "yzm.aspx?id=" +math.random ());        }    </script>

B/S----file upload, image watermark, verification code

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.