ASP.NET/VB.NET 文件上传控件
我的 FileUpload 有问题,当我从本地计算机选择文件时,它不会带来文件的真实路径,它会使用项目文件的路径并假设我选择的文件在那里,有什么想法吗?
例子: 文件名为“Q.JPG”,位于“C:\”中 当我浏览到“C:\”并选择“Q.JPG”并单击“打开”时,出现以下错误 找不到文件“C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\q.jpg”。 例如,当我启动将文件上传到 FTP 的代码时,它将返回错误,因为文件不存在
HTML 端:
<asp:FileUpload ID="FU" runat="server" Height="24px" />
下面是 VB 代码:
Protected Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
If FU.PostedFile IsNot Nothing AndAlso FU.PostedFile.FileName <> "" Then
Dim MaxSize As Integer = FU.PostedFile.ContentLength
If MaxSize > "2097152" Then
lblUpload.Text = "The file size cannot exceed 2 MB"
btnSave.Focus()
GoTo 99
End If
'--------------------------
' set up request...
Dim LocFile As String = FU.PostedFile.FileName
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://myftp.com/" & LocFile), System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential("username", "password")
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes(FU.PostedFile.FileName)
' upload file...
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
'--------------------------
lblUpload.Text = "Uploaded"
btnSave.Focus()
Else
lblUpload.Text = "Choose a file to upload"
btnSave.Focus()
End If
99: 'Do Nothing
End Sub
I have a problem with FileUpload, when I select a file from the local machine, it will not bring the real path of the file, it will use the path for the project files and assume the file I am selecting is there, any ideas?
Example:
File name is "Q.JPG" and is in "C:\"
when I browse to "C:\" and select "Q.JPG" and click open, I get the following Error
Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\q.jpg'.
So when I fire up the code for Uploading the file to FTP for example, it will return an error because file doesn't exist
HTML side:
<asp:FileUpload ID="FU" runat="server" Height="24px" />
Below is the VB code:
Protected Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
If FU.PostedFile IsNot Nothing AndAlso FU.PostedFile.FileName <> "" Then
Dim MaxSize As Integer = FU.PostedFile.ContentLength
If MaxSize > "2097152" Then
lblUpload.Text = "The file size cannot exceed 2 MB"
btnSave.Focus()
GoTo 99
End If
'--------------------------
' set up request...
Dim LocFile As String = FU.PostedFile.FileName
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://myftp.com/" & LocFile), System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential("username", "password")
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes(FU.PostedFile.FileName)
' upload file...
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
'--------------------------
lblUpload.Text = "Uploaded"
btnSave.Focus()
Else
lblUpload.Text = "Choose a file to upload"
btnSave.Focus()
End If
99: 'Do Nothing
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您尝试将 PostedFile 作为本地文件(在 Web 服务器上)读取,而不是从附加到 FileUploader 的 HttpPostedFile 对象读取。
尝试:
The problem is you're trying to read in the PostedFile as a local file (on the web server), not from the HttpPostedFile object attached to the FileUploader.
Try:
我尝试了一些东西,它起作用了..
它起作用了..只需将文件从 FU (FileUpload) 保存到 C:\ ,然后将地址设置为始终从 C:\ 开始
I tried something, and it worked..
It worked.. simply saved the file from FU (FileUpload) to C:\ and then setting the address to always start at C:\