ASP.NET makes uploading files from the client to the server a snap(UploadInterface.PostedFile.SaveAs)

http://www.computerbooksonline.com/tips/asp11.asp
In a previous tip, we talked about uploading files via classic ASP. As you saw, getting the file from the client to the server isn't a big deal, but getting ASP to handle the file properly, once delivered, was no walk in the park. Fortunately, like most other tasks that were once tedious or impossible in ASP, ASP.NET makes this task simple.

All you have to do is provide the interface for the upload, in the same way you would do with ASP--the tag corresponds in ASP.NET to a server-side System.Web.UI.HtmlControls.HtmlInputFile control, which is rendered in HTML in exactly the same way. Then, in your code-behind page, use something like the following intuitive code:

Protected Sub UploadButton_OnClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles UploadButton.Click
Dim FileUpload As HttpPostedFile
Dim UploadedFileName As String

'Get the file from the HtmlInputFile control
FileUpload = UploadInterface.PostedFile

If Not UploadInterface.PostedFile Is Nothing Then
'Get just the filename (without the client path)
UploadedFileName =
FileUpload.FileName.Substring(FileUpload.FileName.LastIndexOf("\"))
'Save it in a safe place on the server
UploadInterface.PostedFile.SaveAs("c:\temp\" & UploadedFileName)
End If
End Sub

原文地址:https://www.cnblogs.com/cy163/p/280709.html