File Upload:
Controls used: FileUpload
When using the idea:
1, determine whether the user selected files
Fileupload.filename Gets the selected file name, determines the length, if the length is greater than 0 means that the file has been selected
JS End: Gets the control by ID and then the value of the control gets the selected file name
2. Save the file to the server
Fileupload.saveas ("absolute path");
3. Get absolute path
Write a relative path first: "Uploads/abc.txt", for example
Then map the path to absolute path: Server.MapPath ("Uploads/abc.txt");
4, keep the original file type and file name
The relative path is changed to "uploads/" + fileupload1.filename;
5, prevent the file name overlay problem
"uploads/" + request.cookies["user"]. Value + DateTime.Now.ToString ("yyyymmddhhmmssms") + fileupload1.filename;
That is, in front of the file name stitching on the login user name and operation time, accurate to milliseconds
6. Restrict user-selectable file types
Add Property accept= ". Jpg,.png,.jpeg,.txt" to the control
However, in the Open dialog box, you can still select all the files, only through the JS side to limit:
document.getElementById ("Button1"). onclick =function () {varFileName = document.getElementById ("FileUpload1"). Value; varName = Filename.substr (Filename.length-4,4); varname1 = Filename.substr (Filename.length-5,5); if(Name! =". jpg"&& Name! =". PNG"&& Name! =". txt"&& name1! =". JPEG") {alert ("Please select the correct file! "); return false; } };
Image plus watermark:
<%@ Page language="C #"autoeventwireup="true"codefile="Shuiyin.aspx.cs"inherits="Shuiyin"%><! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"> <meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/> <title></title>"Form1"runat="Server"> <div> <asp:fileupload id="FileUpload1"runat="Server"/><asp:button id="Button1"runat="Server"text="Button"/> <asp:image id="Image1"runat="Server"/> </div> </form></body>usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;//Be sure to add this quote!usingSystem.Drawing; Public Partial classshuiyin:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {Button1.Click+=button1_click; } voidButton1_Click (Objectsender, EventArgs e) { //get the picture you want to uploadSystem.Drawing.Image img =System.Drawing.Image.FromStream (fileupload1.filecontent); //Add Watermark//Turn The get picture into a GDI drawing surface and start drawingGraphics g =graphics.fromimage (IMG); //corresponding (watermark content, watermark font, watermark color, watermark location)g.DrawString ("ha ha haha",NewFont ("Microsoft Ya-Black", -),NewSolidBrush (color.red),NewPointF (0,0)); //Save Picture//img refers to the folder name, followed by renaming the file to be watermarked stringUL ="img/"+ DateTime.Now.ToString ("yyyymmddhhmmssms") +Fileupload1.filename; //save and generate an absolute pathimg. Save (Server.MapPath (UL)); //show the effect and look at it.Image1.imageurl =ul; }}
File upload, image plus watermark