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?
protected void Button1_Click (object sender, EventArgs e)
{
Label2.Text = Fileupload1.filename;
string filename = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + Label2.Text;
String path = Server.MapPath ("/upload/") + filename;
Fileupload1.saveas (path);
}
(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);
}
}
}
It's mine:
protected void Button8_click (object sender, EventArgs e)
{
foreach (Control Co in Form1. Controls)
{
if (Co is FileUpload)
{
FileUpload Fiup=co as FileUpload;
if (fiup. HasFile)//Key
{
string filename = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + fiup. FileName;
string path = Server.MapPath ("/upload/") + filename;
fiup. 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.
my own;
Page One:
protected void button2_click (object sender, EventArgs e)//upload to Database
{
Imageshujudata da = new Imageshujudata ();
Da. Iname = Fileupload1.filename;
Da. Ipath = fileupload1.filebytes;
Imageshujuda im=new Imageshujuda ();
Im. Insert (DA);
}
protected void Llimage_click (object sender, EventArgs e)//browse pictures from the database
{
Image1.imageurl = "showimage.aspx?id=" +textbox1.text;
}
Page Two:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using DA;
Using Data;
public partial class Showimage:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
string iid=request["id"]. ToString ();
int imid = Convert.ToInt32 (IID);
Imageshujudata da = new Imageshujuda (). Select (IMiD);
if (da! = null)
{
byte[] pic = da. Ipath;
Response.OutputStream.Write (pic,0,pic. Length);
Response.End ();
}
}
}
Database:
drop TABLE imageshuju;--Delete
CREATE TABLE imageshuju--Creation
(
Iid int Identity Primary KEY,
Iname varchar (+) NOT NULL,
Ipath image
)
SELECT * FROM imageshuju;--query
Uploading images and browsing in C#--web