0

I am trying to allow authenticated users to upload pictures to the server through FTP. The code works for the most part. The part that doesn't is that there is an issue in uploading the file. I have tried to upload a few different pictures and all of them are larger on the server and therefore, not properly constructed.

One picture I tried is 4.56MB on my computer and 8.24MB on the server. When I load the picture in Photo, it states "We can't open this file." The page location is at http://troop7bhac.com/pages/slideshowedit.aspx. The following is the VB.NET code behind the upload:

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

The files are obtained through an ASP.NET FileUpload control. The control has been set to allow multiple files at once.

Any help to figure out why the pictures are not uploading properly would be greatly appreciated.

EDIT: I tried Martin Prikryl's possible duplicate solution. Had to change it from C# to VB.NET. It failed. I tried David Sdot's solution and it also failed. Both solutions returned the same errors.

If the page was ran on my local machine, it returned "C:\Program Files (x86)\IIS Express\PictureName.JPG." If the page was ran on the server, it returned "C:\Windows\SysWOW64\inetsrv\PictureName.JPG." Both errors are of the System.IO.FileNotFoundException class.

Tytus Strube
  • 27
  • 1
  • 7
  • If you still have problems, post [mcve]. A simple console application, no ASP. + I do not understand the added section about paths and `FileNotFoundException` - how does it relate to the question? – Martin Prikryl May 04 '17 at 19:53

1 Answers1

1

Your Problem is here:

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

Your image is read as text. From this text you get the bytes UTF8 byte values, thats why your image is nearly twice the size when uplaoded. You need the bytes from the image, without converting them to something else.

fileBytes = File.ReadAllBytes(file.FileName)
David Sdot
  • 2,343
  • 1
  • 20
  • 26