Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File upload unsuccessful for text files

Status
Not open for further replies.

vilrbn

Programmer
Oct 29, 2002
105
FR
Hello,

I'm using this code to copy binary files to my IIS Server:

Dim imgStream As Stream = ImageFile.PostedFile.InputStream
Dim imgData(ImageFile.PostedFile.ContentLength) As Byte
Dim intFileNameLength As Integer
Dim strFileName As String
Dim Filename As File
Dim strFileNamePath As String

' read uploaded file in the buffer using InputStream object
imgStream.Read(imgData, 0, ImageFile.PostedFile.ContentLength)
'full path to uploaded file on client’s system
strFileNamePath = ImageFile.PostedFile.FileName
intFileNameLength = InStr(1, StrReverse(strFileNamePath), "\")
strFileName = Mid(strFileNamePath, (Len(strFileNamePath) - intFileNameLength) + 2)
WriteToServer(ConfigurationSettings.AppSettings("TempServerPath") + strFileName, imgData)


Public Sub WriteToServer(ByVal strPath As String, ByRef Buffer As Byte())
'copy the selected file to the Server
Dim myFileStream As FileStream
Dim NewFile As BinaryWriter
myFileStream = File.Create(Path:=strPath)

Try
'Create a file
NewFile = New BinaryWriter(output:=myFileStream)
'Write data to the file
NewFile.Write(Buffer, 0, Buffer.Length)
Catch exc As Exception
lblMsg.Text = &quot;<SCRIPT language = 'javascript'>alert('Error: &quot; & &quot;\n\n&quot; & exc.Message & &quot;');</SCRIPT>&quot;
Finally
'Close file
myFileStream.Close()
End Try
End Sub

It's working fine for binary files (*.jpg,*.gif,*.png...) but for text ones (*.htm, *.txt...), it's copying an empty file to my TempServerPath directory.

Anybody has an idea why ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top