Webfrom Upload a single upload multiple uploads

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.


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

Remove from the Image field in the database and display it on the page.

Webfrom Upload a single upload multiple uploads

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.