ASP.NET/VB.NET 文件上传控件

发布于 2024-11-04 01:38:19 字数 1833 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2024-11-11 01:38:19

问题是您尝试将 PostedFile 作为本地文件(在 Web 服务器上)读取,而不是从附加到 FileUploader 的 HttpPostedFile 对象读取。

尝试:

Dim objFileStream As System.IO.Stream = FU.PostedFile.InputStream
Dim bFile(objFileStream.Length) As Byte
objFileStream.Read(bFile, 0, objFileStream.Length)

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)
舂唻埖巳落 2024-11-11 01:38:19

我尝试了一些东西,它起作用了..

            FU.SaveAs("C:\" & FU.FileName)

            '--------------------------
            ' set up request...

            Dim LocFile As String = FU.PostedFile.FileName
            Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("MyFTP.com" & LocFile), System.Net.FtpWebRequest)

            clsRequest.Credentials = New System.Net.NetworkCredential("username", "password")
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

它起作用了..只需将文件从 FU (FileUpload) 保存到 C:\ ,然后将地址设置为始终从 C:\ 开始

I tried something, and it worked..

            FU.SaveAs("C:\" & FU.FileName)

            '--------------------------
            ' set up request...

            Dim LocFile As String = FU.PostedFile.FileName
            Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("MyFTP.com" & LocFile), System.Net.FtpWebRequest)

            clsRequest.Credentials = New System.Net.NetworkCredential("username", "password")
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

It worked.. simply saved the file from FU (FileUpload) to C:\ and then setting the address to always start at C:\

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文