ASP.NET/VB.NET FileUpload控件 [英] ASP.NET/VB.NET FileUpload Control
问题描述
我有一个文件上传一个问题,当我从本地机器选择一个文件,它不会带来真正的路径的文件,它将使用路径为项目文件,并假定我选择的文件是存在的,任何想法?
例如:
文件名是Q.JPG,是在C:\\
当我浏览到C:\\,然后选择Q.JPG,并点击打开,我得到以下错误
找不到文件'C:\\ Program Files文件\\微软的Visual Studio 8 \\ Common7 \\ IDE \\ q.jpg。
所以,当我火了code的文件上传到FTP为例,由于文件不存在,它会返回一个错误
HTML端:
< ASP:文件上传ID =FU=服务器HEIGHT =24px的/>
下面是VB code:
保护小组btnUpload_Click(BYVAL发件人为System.Object的,BYVAL E上System.EventArgs)把手btnUpload.Click 如果FU.PostedFile IsNot运算没有AndAlso运算FU.PostedFile.FileName<> 然后
昏暗MAXSIZE作为整数= FU.PostedFile.ContentLength
如果MAXSIZE> 2097152接着
lblUpload.Text =文件大小不能超过2 MB
btnSave.Focus()
转到99
万一
--------------------------
建立的请求......
昏暗LOCFILE作为字符串= FU.PostedFile.FileName
昏暗clsRequest作为System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(ftp://myftp.com/&放大器; LOCFILE),System.Net.FtpWebRequest)
clsRequest.Credentials =新System.Net.NetworkCredential(用户名,密码)
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile 读文件...
昏暗BFILE()作为字节= System.IO.File.ReadAllBytes(FU.PostedFile.FileName) ' 上传文件...
昏暗clsStream作为的System.IO.Stream = clsRequest.GetRequestStream()
clsStream.Write(BFILE,0,bFile.Length)
clsStream.Close()
clsStream.Dispose()
--------------------------
lblUpload.Text =上传
btnSave.Focus()
其他
lblUpload.Text =选择要上传的文件
btnSave.Focus()
万一
99:'你没事
结束小组
问题是你想在PostedFile读为本地文件(Web服务器上),而不是从连接到FileUploader的HttpPostedFile对象。
尝试:
昏暗objFileStream作为的System.IO.Stream = FU.PostedFile.InputStream
昏暗BFILE(objFileStream.Length)为字节
objFileStream.Read(BFILE,0,objFileStream.Length)
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
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:
Dim objFileStream As System.IO.Stream = FU.PostedFile.InputStream
Dim bFile(objFileStream.Length) As Byte
objFileStream.Read(bFile, 0, objFileStream.Length)
这篇关于ASP.NET/VB.NET FileUpload控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!