. net file uploads-small data--un

Source: Internet
Author: User

File Upload
Control:
FileUpload-Controls, Interface + Methods + Properties
Button/linkbutton/imagebutton

FileUpload CONTROLS:
1.SaveAs ("absolute path to upload to the server") method: Upload the file.
The conversion between the relative path and the absolute path is required using Server.MapPath ().

2.FileName properties: The absolute filename of the file to be uploaded without the path.

3.FileBytes Properties: The contents of the uploaded file, that is, binary data.

Scene:
One, the single file upload to the server hard disk
The simplest upload:
String path = Server.MapPath ("Uploads/aaa.txt"); Path mapping required
Fileupload1.saveas (path);
Problem: All uploaded files are called Aaa.txt. How to keep the original name of the file?

Optimize one: Use the FileName property of FileUpload to get the name of the client that uploaded the file.
string fileName = Fileupload1.filename; Get file uploads between the client's name and the child.
String path = Server.MapPath ("uploads/" + fileName);
Fileupload1.saveas (path);
Question: How can I avoid overwriting files if different users upload the same file name?
Optimization Two: Different users, in the same time to pass the same file name.
Use the user name in the file name to differentiate:
String fileName = DateTime.Now.ToString ("Yyyymmddhhmmss") +session["user"]. ToString () + fileupload1.filename; Get file uploads between the client's name and the child.
String path = Server.MapPath ("uploads/" + fileName);
Fileupload1.saveas (path);
Problem: Uploading files larger than 4M will cause an error. The maximum size of the default upload file is 4096K
Optimization three: capacity expansion.
Configure the maximum length of the upload request in Web. config.
<system.web>
</system.web>

Job: Go back to find resources: How does C # upload large files?

Second, multiple files uploaded to the server hard disk
(a) Simple implementation:
Idea: traverse each control in the form of the page, determine whether it is fileupload, if it is, force it into the FileUpload type, and then upload it individually by single file upload.
int i = 1; The serial number of the uploaded file
foreach (Control ctrl in This.form1.Controls)//iterate through each control in the form
{
If (Ctrl is FileUpload)//See if the Ctrl object is not FileUpload type
{
FileUpload file = (FileUpload) ctrl;//forced conversion to FileUpload type

Single-File Upload code
String fileName = DateTime.Now.ToString ("yyyymmddhhmmssms") +i.tostring ("0000") + file. FileName;
String path = Server.MapPath ("uploads/" + fileName);
File. SaveAs (path);

i++;
}
}
Issue: A 0k file will also be generated on the server side without the option to upload the file.

(b) Optimization: To skip the upload of files without the option to jump over.
int i = 1;
foreach (Control ctrl in This.form1.Controls)
{
If (Ctrl is FileUpload)//See if the Ctrl object is not FileUpload type
{
FileUpload file = (FileUpload) Ctrl;

if (file. HasFile)//Determines whether the uploaded file is selected in each control.
{
String fileName = DateTime.Now.ToString ("yyyymmddhhmmssms") + i.tostring ("0000") + file. FileName;
String path = Server.MapPath ("uploads/" + fileName);
File. SaveAs (path);

i++;
}
}
}
Third, upload the file to the database image field
The first step: Take the binary data of the file. Fileupload1.filebytes (byte[])
Step two: Send to the database. Ado. NET or Linq2sql


Remove from the Image field in the database and display it on the page.
1. Choose a page to display the image. For example: Showpic.aspx.
This page, based on the primary key values, queries the image data and response.outputstream the interface.
protected void Page_Load (object sender, EventArgs e)
{
if (request["id"]! = NULL)
{
String index = request["id"]. ToString ();
The main things to do are:
1. Read the data record of a photo table.
var query = _context.photo.where (p = = p.ids.tostring () = = index);
Photo data = query. First ();
byte[] pic = data. Content.toarray ();
2. Export the binary data directly to the interface.
Response.OutputStream.Write (pic, 0, pic. Length); outputting binary data to the output stream
Response.End ();
}
}
2. On another interface, place an image control that imageurl the image control to the page above and pass the specified value past.
HTML code:
<asp:dropdownlist id= "DropDownList1" runat= "Server" >
</asp:DropDownList>
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "Display"/>
<br/>
<asp:image id= "Image1" runat= "Server"/>
C # code:
Fills the drop-down list.
The code on the show button
protected void Button1_Click (object sender, EventArgs e)
{
Image1.imageurl = "showpic.aspx?id=" + dropdownlist1.selectedvalue;
}


How do I verify it?
Ideas:
1. Two pages are also required. A page is used to draw a verification code, and the other page is used to display text boxes and validations.
2. Use the knowledge points in the drawing technique to draw a random number picture. Random numbers need to be saved in the session, check that the verification code is correct to use the value in the session to compare.
3. Click on the verification code refresh how to implement?
function Dochange (IMG) {
var temp = Math.random (); JS generates random numbers of code.
Img.setattribute ("src", "yzm.aspx?id=" +temp);
}

Job: Find code for C # to generate a verification code.

. net file uploads-small data--un

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.