File Upload:
Turns the relative path into an absolute path.
String absolutepath = Server.MapPath (RelativePath);
FileUpload CONTROLS:
Property:
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?
(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: Garbage files are generated
Generate a JPG on the server and assign the path of this JPG to the image control
Law II: Make a separate page to display the binary data of the picture. Assign this page to the image control.
Uploading images and watermarks:
First, 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 ("Comic Sans MS", 18);
3. Locate the image drawing area
Graphics g = Graphics.fromimage (IMG);
g.DrawString ("http://www.itNBA.com", font, brush, 0, 0);
Third, the picture object to save to the hard drive up
string fileName = Fileupload1.filename;
String path = Server.MapPath ("uploads/" + fileName);
Img. Save (path);
File Upload & Draw watermark