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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file upload gives an error when upload file with query string

Status
Not open for further replies.

lexx2gee

Programmer
Mar 1, 2005
56
GB
following code i am using to upload file

<script runat="server" language="VB">
Dim sFullPath
sub btnSubmit_click(byval sender as system.object, byval e as eventArgs)

If Len(File1.PostedFile.FileName) > 0 then
Dim sPath, sPathFriendly, sFile, sSplit
sPath = Server.MapPath(".")
If Right(sPath, 1) <> "\" Then
sPathFriendly = sPath 'Friendly path name for display
sPath = sPath & "\"
Else
sPathFriendly = Left(sPath, Len(sPath) - 1)
End If
sFile = File1.PostedFile.FileName
sSplit = Split(sFile, "\")
sFile = sSplit(UBound(sSplit))

sPath = "D:\temp\"
sFullPath = sPath & sFile

File1.PostedFile.SaveAs(sFullPath)
file_panel.visible=false

msg.text="<font color=red><b>Your file ( " & sFile & ") has been uploaded successfully.</b></font>"

end if


end sub
</script>

This code works fine when we use without query string e.g but when i use something like
it gives me syste.nullreference error

I am using multipart/form-data with all of my form
You can test page on

(upload file will work)
(upload file will not work)

any response will be appreciated
 
How are you using the query string ?

Also, instead of: Len(File1.PostedFile.FileName) > 0
do: file1.hasfile = true
 
When i use without query string it finds the File1 object but when i use with query string it does not find the File1 object.

Will you please go to the page links i gave and you will see how i am using query string

Thanks for your response

 
Strange behaviour...

With QS it stuck first:
If Len(request("file1")) > 0 then
and then:
File1.PostedFile.SaveAs(sFullPath)


It also gave an error without the qs at my first attempts and now it seems to upload it and redirects to the same page (i dont see the red messsage).




 
Thanks TipGiver,
i have tried this code

If Len(request("file1")) > 0
response.write(File1.PostedFile)
response.end
end if

but it does not not dislay anything according to my mind it should display the following string and thats why i am getting error

System.Web.HttpPostedFile
 
1. What do you want to do. A simple upload?
2. Where to save the file?
3. You want to save it with the same name ?
4. What is the version of ASP.NET ? (1.1 or 2.0)

?
 
1) Yeas i want simple upload
2) i want to save on server
3) yes with the same name
4) 1.1


 
Have a look at this and tell me if it works

Code:
    Protected Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles UploadButton.Click

        With FileUpload1

            If .HasFile Then

                Dim _i As Integer = .FileName.LastIndexOf("\")
                Dim _fn As String = .FileName.Substring(_i + 1)

                Dim _dsf As String = Server.MapPath("~/upload") & "//" & _fn

                If Not IO.File.Exists(_dsf) Then

                    Try

                        .SaveAs(_dsf)
                        Response.Write("<br><b>Upload successful!</b>")

                    Catch ex As IO.IOException

                        Response.Write("<br><b>IO exception</b>")

                    Catch ex As Exception

                        Response.Write("<br><b>General exception</b>")

                    End Try

                Else

                    Response.Write("<br><b>The file exists!</b>")

                End If

            Else

                Response.Write("<br><b>Please select a valid file...</b>")

            End If

        End With

    End Sub
 
Forgot to say the i upload the file to a folder "upload" that relies on the root ("~")

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top