我試圖讓認證用戶通過FTP上傳圖片到服務器。代碼大部分適用。不是的部分是上傳文件時出現問題。我試圖上傳一些不同的圖片,並且它們都在服務器上較大,因此沒有正確構建。VB.NET FTP圖片上傳錯誤
我試過的一張照片是我的電腦上的4.56MB和服務器上的8.24MB。當我在照片中加載圖片時,它指出「我們無法打開此文件。」頁面位置在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類。
的可能的複製[Zip文件越來越在C#從服務器上下載後損壞(http://stackoverflow.com/questions/41357267/zip-file-is-getting-corrupted-after-downloading-from -server-in-c-sharp) –
如果仍有問題,請發表[mcve]。一個簡單的控制檯應用程序,無ASP。 +我不明白關於路徑和'FileNotFoundException'的附加部分 - 它與問題有什麼關係? –