VB.NET FTP图片上传错误

时间:2017-05-04 07:17:55

标签: asp.net vb.net file-upload ftp

我正在尝试允许经过身份验证的用户通过FTP将图片上传到服务器。该代码大部分都适用。没有的部分是上传文件时出现问题。我试图上传一些不同的图片,所有这些图片在服务器上都比较大,因此构造不正确。

我试过的一张图片在我的电脑上是4.56MB,在服务器上是8.24MB。当我在Photo中加载图片时,它会显示“我们无法打开此文件”。页面位置位于http://troop7bhac.com/pages/slideshowedit.aspx。以下是上传背后的VB.NET代码:

Sub uploadFile_Click(sender As Object, e As EventArgs)
    lblUploadErrors.InnerHtml = ""
    If (lstSlideshowChoose.SelectedValue = "") Then
        lblUploadErrors.InnerHtml = "<p>A slideshow must be selected.</p>"
    Else
        If (FileUpload1.HasFile) Then
            Dim nameList() As String
            Dim successList() As String
            Dim i As Integer = 0

            For Each file As HttpPostedFile In FileUpload1.PostedFiles
                Dim fileBytes() As Byte = Nothing
                Dim fileName As String = Path.GetFileName(file.FileName)
                Dim photoRE As New Regex("^[A-z0-9 _]{1,}\.jpg|JPG|jpeg|JPEG|png|PNG+$")
                Dim photoSuccess As Boolean = photoRE.Match(fileName).Success

                ReDim Preserve nameList(i)
                ReDim Preserve successList(i)

                If (photoSuccess = True) Then
                    Using fileStream As New StreamReader(file.InputStream)
                        fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd())
                        fileStream.Close()
                    End Using
                    Try
                        Dim request As FtpWebRequest = DirectCast(WebRequest.Create(ftpPath & lstSlideshowChoose.SelectedValue & "/" & fileName), FtpWebRequest)

                        request.Method = WebRequestMethods.Ftp.UploadFile
                        request.Credentials = New NetworkCredential(ftpUser, ftpPass)
                        Using uploadStream As Stream = request.GetRequestStream()
                            uploadStream.Write(fileBytes, 0, fileBytes.Length)
                            uploadStream.Close()
                        End Using

                        Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)

                        response.Close()
                        successList(i) = "Success "
                    Catch ex As Exception
                        successList(i) = "Failed "
                    End Try
                Else
                    successList(i) = "Failed "
                End If
                nameList(i) = fileName
                i += 1
            Next
            For x As Integer = 0 To nameList.Count - 1
                lblUploadErrors.InnerHtml += "<p>" & successList(x) & nameList(x) & "</p>"
            Next
        Else
            lblUploadErrors.InnerHtml = "<p>You have not selected a picture to upload.</p>"
        End If
    End If
End Sub

这些文件是通过ASP.NET FileUpload控件获得的。控件已设置为允许一次存在多个文件。

任何有助于弄清楚图片无法正常上传的帮助都将非常感激。

编辑:我尝试了Martin Prikryl可能的重复解决方案。不得不将它从C#改为VB.NET。它失败了。我尝试了David Sdot的解决方案,它也失败了。两种解决方案都返回了相同的错误。

如果页面在我的本地计算机上运行,​​则返回“C:\ Program Files(x86)\ IIS Express \ PictureName.JPG”。如果页面在服务器上运行,则返回“C:\ Windows \ SysWOW64 \ inetsrv \ PictureName.JPG”。这两个错误都是System.IO.FileNotFoundException类。

1 个答案:

答案 0 :(得分:1)

你的问题在这里:

Using fileStream As New StreamReader(file.InputStream)
    fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd())
    fileStream.Close()
Using

您的图片将被视为文字。从这个文本中你可以得到字节UTF8字节值,这就是为什么你的图像几乎是上传时的两倍大小。您需要图像中的字节,而不将其转换为其他内容。

fileBytes = File.ReadAllBytes(file.FileName)