VS 2008 [RESOLVED] Help with File Upload and Deployment-VBForums
Results 1 to 20 of 20

Thread: [RESOLVED] Help with File Upload and Deployment

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Resolved [RESOLVED] Help with File Upload and Deployment

    So I have my first ASP website project I want to deploy but need some help finishing up

    My project will read a txt file database on users computer
    using file upload:

    Code:
    If FileUpload1.HasFile Then
                Try
                    FileUpload1.SaveAs("C:\Uploads\" & _
                       FileUpload1.FileName)
                    Label1.Text = "File name: " & _
                       FileUpload1.PostedFile.FileName & "<br>" & _
                       "File Size: " & _
                       FileUpload1.PostedFile.ContentLength & " kb<br>" & _
                       "Content type: " & _
                       FileUpload1.PostedFile.ContentType
                Catch ex As Exception
                    Label1.Text = "ERROR: " & ex.Message.ToString()
                End Try
            Else
                Label1.Text = "You have not specified a file."
            End If
    Then I want to pass the file through a TextFieldParser function to create a table

    Code:
     Dim safefilename As String = FileUpload1.FileName
    
            Dim sfilename As String = ("C:\Uploads\emailtest.txt")
    
            ReadTable(sfilename)
    The porblem I am having is passing the file

    the first line Dim safefilename does not work

    if I hard code the file location as in second line Dim sfilename it works

    So I obviously need help getting the file properly

    Also when I deploy the project on my webhost the file location will obviously be different and I am not sure how that works. Any help would be appreciated I have a little experience with vb.net but I am brand new to this web stuff

    Thanks

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,108

    Re: Help with File Upload and Deployment

    Ok, what i think you are doing is trying to work this from your PC?
    That should work because you are in the same host as your application but you will get problems when you deploy. In short, you cannot just get the file from a client with a path. What you get now is your own PC path but that is not the same with a server application.
    I made a post about this a couple of months ago but Mr slow as hell is searching 10minutes now for results, so i currently cannot post it.
    Slow as hell.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with File Upload and Deployment

    Quote Originally Posted by sapator View Post
    Ok, what i think you are doing is trying to work this from your PC?
    That should work because you are in the same host as your application but you will get problems when you deploy. In short, you cannot just get the file from a client with a path. What you get now is your own PC path but that is not the same with a server application.
    I made a post about this a couple of months ago but Mr slow as hell is searching 10minutes now for results, so i currently cannot post it.
    Thank you, yes but basically you just reiterated my question
    "Also when I deploy the project on my webhost the file location will obviously be different and I am not sure how that works. Any help would be appreciated I have a little experience with vb.net but I am brand new to this web stuff"

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,108

    Re: Help with File Upload and Deployment

    try playing with this:
    Dim appPath As String = Request.PhysicalApplicationPath
    Slow as hell.

  5. #5
    New Member
    Join Date
    Mar 2013
    Posts
    1

    Re: Help with File Upload and Deployment

    Initializing a string variable can be tricky sometimes. Instead of defining safefilename in the initialization process, try this and let me know if it works out for you.


    Dim safefilename As String

    'Add this to your event handler
    safefilename = FileUpload1.FileName

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with File Upload and Deployment

    Thanks for the suggestions guys but neither of those work
    Request.PhysicalApplicationPath as implied returns the path the application is running on

    changing

    Dim safefilename as string= FileUpload1.FileName

    to

    Dim safefilename as String option didnt work either

    This must be easier then this i cant see what i am missing

  7. #7
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,601

    Re: Help with File Upload and Deployment

    Define "Not Working". That's not very helpful. Does it throw an exception? If so, what exception? Is the FIleUpload1.FileName field empty? If so, where are you opening the FileUpload.

    Here is code I have used before to upload a file with a FIleUpload. Not really much to it. Just make sure you have write access to the folder for whatever account you are running the IIS AppPool with for the site.

    Code:
                    Dim savePath As String = "~\Uploads\" & fuDocuments.FileName
                    Dim strFilePath As String = Server.MapPath(savePath)
    
                    ' If the FileExists, Delete it before saving it
                    If System.IO.File.Exists(strFilePath) Then
                        System.IO.File.Delete(strFilePath)
                    End If
    
                    fuDocuments.SaveAs(strFilePath)
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with File Upload and Deployment

    The FileUpload File name is empty
    I am, trying to open it in another sub the uses a function to return a table

    Dim safefilename As String = FileUpload1.FileName

    ReadTable(safefilename)

    If i hard code the file location it will work. Is that the way it needs to get done? I for asome reason thought I could just call the Fileupload File ?

    How would i save it to the server? right now i am just saving it to my hard drive on my computer?

  9. #9
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,108

    Re: Help with File Upload and Deployment

    Both solutions posted (not the string one) should work fine. If you say you get an empty filename then you probably, somehow , cause a postback on the page and your value get's reset.
    Slow as hell.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with File Upload and Deployment

    Which ones should work if not the string one, I only see a string solution??

    How do i know if I am causing a postback?

  11. #11
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,108

    Re: Help with File Upload and Deployment

    I meant the dim as string and the string=fileupload.hasfile. That is pointless.
    I made a mistake and i did not read that this is your first project and your reputation made me think that you had some experience with asp.net but since you are asking basic stuff like postbacks, i really think that you should post the portions of the code that u are using to make the hasfile. Not all the code of course and this time please post entire subs so we may know under which sub and controls you run your code. Also post you page_load event code, if you use any.
    Also as a note, my opinion is that you must do a little reading on the basics of asp.net because we may fix this one but there are similar issues with the basic and you will soon find another problem. At the very least, you must read about the page life cycle, get and post, sessions and web.config usage. Next if you know that your will need to design part of a page, yourself you must read about HTML and CSS. Finally it's java, ajax and Jquery but i would omit those for now.
    Last edited by sapator; Mar 18th, 2013 at 02:59 AM.
    Slow as hell.

  12. #12
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,601

    Re: Help with File Upload and Deployment

    Quote Originally Posted by billboy View Post
    The FileUpload File name is empty
    I am, trying to open it in another sub the uses a function to return a table

    Dim safefilename As String = FileUpload1.FileName

    ReadTable(safefilename)

    If i hard code the file location it will work. Is that the way it needs to get done? I for asome reason thought I could just call the Fileupload File ?

    How would i save it to the server? right now i am just saving it to my hard drive on my computer?
    SO when you click Save, on the screen, your file upload control has a file name displayed in it, but when get to the point in the code to Save it is empty? A postback shouldn't clear a FIleUpload itself, but is there something in your Page Load event that may be clearing it?
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with File Upload and Deployment

    Thanks I do have a little bit of experience with VB.NET and have written a few small programs. In fact I have created this program in .Net and now trying to make a web version of it
    I have been reading and watching video tutorials over at the ASP.NET site. There is a video on page life cycle that I am going to watch in a little bit

    Here is my code:

    Code:
    Protected Sub upload_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles upload_Button.Click
            If FileUpload1.HasFile Then
                Try
                    FileUpload1.SaveAs("C:\Uploads\" & _
                       FileUpload1.FileName)
                    Label1.Text = "File name: " & _
                       FileUpload1.PostedFile.FileName & "<br>" & _
                       "File Size: " & _
                       FileUpload1.PostedFile.ContentLength & " kb<br>" & _
                       "Content type: " & _
                       FileUpload1.PostedFile.ContentType
                Catch ex As Exception
                    Label1.Text = "ERROR: " & ex.Message.ToString()
                End Try
            Else
                Label1.Text = "You have not specified a file."
    
            End If
        End Sub
    This works fine and does save the file.

    I now want ot be able to access the file for my TextFieldParser, this is where I am stuck

    Code:
     Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim str As String = ("F:\email\emailtest.txt")        I have to hard code the file
            ReadTable2(str)
    I wanted to be able to do something like

    str = Fileupload1.FileName
    Last edited by billboy; Mar 18th, 2013 at 11:38 PM.

  14. #14
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,108

    Re: Help with File Upload and Deployment

    Ok, (i have to sleep but will post this if anyone wants to help, or i will post again tomorrow).
    So you upload the file successfully and the page cycle ends.
    You now want to access the file and you click another button(?). Well tough luck, you started a new postback and it CLEARS the FIleUpload.
    You have some options here.
    You put the uploaded file location, immediately after the upload, to a session, viewstate, or cookie.
    You save the location of the file to a db or xml file and you request it from there.
    You use your TextFieldParser inside the upload_Button_Click sub after you upload your file (not sure what ReadTable2 do, so i don't know if it will work).
    Sleepy time.
    Slow as hell.

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with File Upload and Deployment

    Thank you
    I am kind of learning as I go here. So it clears makes sense. I will read up on session and view state ironically those are the topics for tonights videos on ASP.NET tutorial videos I have been watching.

    Is there a preferred method or best practice for something like this, this has to be fairly common task

    Thanks again for your help

  16. #16
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,108

    Re: Help with File Upload and Deployment

    Hi. It really depends. In sort a viewstate will keep the information on your web page, while a session will keep it on server memory( or, if i am not wrong, a predefined DB, if you set it that way (never tried)).
    Viewstate creates a lot of xtra data in your page, so if you save, p.e. a datatable in viewstate you will have a veeery big page source.Session will keep the data in server memory so you must be careful not to abuse the feature.
    If all you need to do is keep a small string value temporarily then either way is fine. Just remember to clear the value when you are done with it.
    Slow as hell.

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with File Upload and Deployment

    I saved the file name and path to a HiddenFiled value at the time of fileupload this seems to persist the path and is working thus far

    Is there anything wrong with doing it this way?

  18. #18
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,108

    Re: Help with File Upload and Deployment

    Nop, it's fine but you don't have any security. If you don't care about that you are ok.
    Slow as hell.

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with File Upload and Deployment

    Its just the file name of the file the user uploaded to begin with so i dont think security is an issue in this case

    Thanks

  20. #20
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,108

    Re: Help with File Upload and Deployment

    No problem, happy to help.
    Please mark the thread resolved if you are done with it.
    Slow as hell.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.