How to upload a file to a web server




















The code in this section retrieves the file from the local file system, checks to see if the file already exists on the server, and then uploads the file to the Web site. To add this code, follow these steps:. Double-click the Upload button that was created in the Add a Button control section of this article to create an event handler for the Click event of the button control. Add the following code to the Click event handler for the Upload button:. On the Debug menu, click Start to build and to run the application.

A text box and a command button appear. Type the path of the image file in the text box, or click Browse to locate the image file on your local computer. Click Upload to send the file to the server. If the file is unique, you receive a message that the upload succeeded. If the file already exists on the server, you receive an appropriate message. To make this application work in the. To do this, follow these steps:. Locate the application folder in Windows Explorer. Right-click the ApplicationName folder, and then click Properties.

The ApplicationName Properties dialog box appears. The maximum size file that can be uploaded depends on the value of the MaxRequestLength configuration setting. If users attempt to upload a file that is larger than the maximum, the upload fails. Add a FileUpload control to the page. For security reasons, you cannot pre-load the name of a file into the FileUpload control. In a handler for an event, such as the page's Load event, do the following:.

Check that the FileUpload control has an uploaded file by testing its HasFile property. Check the file name or MIME type of the file to make sure that users have uploaded a file that you want to accept. MIME types for uploaded files can be spoofed under some circumstances, so checking the file's MIME type alone is not a reliable security check.

Save the file to a location you specify. You also manually type a path, however, clicking the Browse button is strongly recommended to avoid typing incorrect value.

Click the Browse button to choose a file. The browser automatically opens the Browse window if you try to type anything in the text box. Instead, it only contains a button that reads Choose File allowing the user to select a file.

After selection, the file name is displayed in the text box on the side. It does not contain any text box and user must click the Choose File button. Once a file is selected, click Upload file. Quick Links.



0コメント

  • 1000 / 1000