Upload and download of files

xiaoxiao2021-03-06  50

In web programming, we often need to upload some local files to the web server. After uploading, users can easily browse these files through the browser, and the application is very widespread.

So use C # how to implement file upload function?

First, add a Uploaded Web Form in your Visual C # Web Project, in order to upload files, you need to select the File Field control of the HTML class in Toolbox, add this control to the web form, however, the control also Not a server control, we need to add the following code: so it becomes a server control, if you need to upload files simultaneously We can add this control accordingly.

It should be noted that the code must be set to the

in the code:

If there is no this property, it cannot be uploaded.

Then add a Web Form class to the Button to add the following code to the following code in this Web Form.

// Upload the image of the picture DateTime now = DateTime.now; // Take the time to the DataTime class in the object now String strbaselocation = "d: // web // fc // pic //" This is a file The absolute directory IF (UploadFile1.postedFile.contentLength! = 0) // determines if the selection dialog is selected for 0 {UPLOADFILE1.POSTEDFILE.SAVEAR.TOSTRING () UPLOADFILE1 .Postedfile.contentLength.toString () "jpg"); // Perform upload, and automatically naming the file according to the date and file size, make sure not repeating the label1.text = "picture 1 has been uploaded, the file name is:" now.dayofyear.toString () UPLOADFILE1.POSTEDFILE.CONTENTLENGTH.TOSTRING () "JPG";

[Precautions] 1. Upload files can not be unlimited;

2. Pay attention to the security of IIS;

3. When making an installer with Visual Studio installation, please pay attention to the absolute path problem where the installer is located;

4. Pay attention to the reusal problem after the file is uploaded.

The download features are as follows:

转载请注明原文地址:https://www.9cbs.com/read-40863.html

New Post(0)