Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6691283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:44:01+00:00 2026-05-26T05:44:01+00:00

This is related to my previous question . An unforeseen issue arose with the

  • 0

This is related to my previous question. An unforeseen issue arose with the Wizard control.

I now know how to upload to FTP, however when using the FileUpload control inside a Wizard control, when you move to the next step, the File you selected gets cleared because of the postback. I need to be able to rename the file according to the results from the Wizard before uploading. So…

  1. I finish my wizard
  2. It uploads some stuff to a database
  3. Renames the file according to those results
  4. Uploads the renamed file to the FTP server

I suspect I will need to follow a procedure something like this, having an upload button next to FileUpload

  1. On “Upload” button click stream the file to the Web Server.
  2. Complete the Wizard.
  3. If the wizard completes successfully, rename file and stream to FTP server.
  4. If the wizard fails, what? Delete the file from the web server? How?

I think I understand the process, so I would like help on how to split my FTP Upload function into two parts with the proper error handling for when the wizard fails.

It would be a great help if you please use the following code as a base. Thanks as always 🙂

Protected Sub UploadFile(ByVal NewFilename As String)
    Dim myFtpWebRequest As FtpWebRequest
    Dim myFtpWebResponse As FtpWebResponse

    'Function one? - Problem, "NewFilename" depends on the output of the Wizard,
    '                but obviously it has not been called yet.
    myFtpWebRequest = CType(WebRequest.Create(ftpServer + ftpPath + NewFilename), FtpWebRequest)
    myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile
    myFtpWebRequest.UseBinary = True

    Dim myFileStream As Stream = FileUpload1.FileContent
    myFtpWebRequest.ContentLength = myFileStream.Length

    'Function two?
    Dim requestStream As Stream = myFtpWebRequest.GetRequestStream()
    myFileStream.CopyTo(requestStream)
    requestStream.Close()

    myFtpWebResponse = CType(myFtpWebRequest.GetResponse(), FtpWebResponse)
    myFtpWebResponse.Close()
End Sub

— ANSWER —

Here’s my final implementation based on input from Icarus 🙂

For brevity I have excluded the error catching.

'This function is what kicks things off...
Protected Sub UploadFileToWebServer() Handles btnUploadFile.Click
    Dim TempDir As String = "C:\TEMP", FileName As String = "uploadedfile.tmp", FilePath As String
    If Not Directory.Exists(TempDir) Then
        Directory.CreateDirectory(TempDir).Attributes = FileAttributes.Directory
    End If
    FilePath = TempDir + "\" + FileName
    Session.Add("FileName", File1.FileName) 'Keep track of uploaded file name
    File1.SaveAs(FilePath)
    Session.Add("File", FilePath)
End Sub

After the file is uploaded to the web server, we can continue through the wizard, and when the “Finish” button is clicked, the wizard data gets submitted to the database. The filename is based on the inserted record ID. The following function gets called by the “Final” button click after the record is inserted, and the file finally gets uploaded to the FTP server with the filename changed accordingly.

Protected Sub UploadFileToFtpServer(ByVal FileLinkStr As String)
    Dim myFtpWebRequest As FtpWebRequest
    Dim myFtpWebResponse As FtpWebResponse

    'Defines the filename, path, and upload method, and connection credentials
    myFtpWebRequest = CType(WebRequest.Create(ftpServer + ftpPath + FileLinkStr), FtpWebRequest)
    'Be sure to authenticate prior to uploading or nothing will upload and no error
    myFtpWebRequest.Credentials = New NetworkCredential(ftpUsername, ftpPassword)
    myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile
    myFtpWebRequest.UseBinary = True

    'Streams the file to the FTP server
    'Retrieves File temporarily uploaded to the Web Server during Wizard Processing
    Dim iStream As New FileInfo(Session.Item("File"))
    Dim myFileStream As Stream = iStream.OpenRead
    myFtpWebRequest.ContentLength = myFileStream.Length
    Dim requestStream As Stream = myFtpWebRequest.GetRequestStream()
    myFileStream.CopyTo(requestStream)
    requestStream.Close()

    myFtpWebResponse = CType(myFtpWebRequest.GetResponse(), FtpWebResponse)
    myFtpWebResponse.Close()
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:44:02+00:00Added an answer on May 26, 2026 at 5:44 am

    Your understanding is correct. Once you upload the file to the web server (you’d need to place it in a temp directory somewhere and keep track of the file name you gave it) and the wizard completes successfully, you grab that file, rename it accordingly and upload it to the ftp server. If fails, simply call:

    File.Delete(Path_to_file_uploaded_on_temp_directory);
    

    You can keep track of the file name given originally, by storing it in Session, for example. When you upload the file to the server initially, do something like Session["FileName"]=Path_to_temp_directory+fileName;

    On the final step of the Wizard, get the file name from Session and either rename it and upload it to the FTP Server or delete it.

    Of course you need to account for possible name conflicts, etc. You can use a Guid to generate a random name for the file, for example.

    I hope I explained this clearly.

    EDIT

    To make sure I understand correctly…

    1. You need your user to go through all the steps of a Wizard kind of thing
    2. During the process, you ask your user to upload a file.
    3. Because the user has to select a file before the last step of the wizard, you are forced to upload the file immediately the user clicks on the “Next” button to go to the next step of the wizard.
    4. At the very last step of the Wizard, you need to determine whether the file the user has selected should be uploaded to an ftp server (presumably, another box different from your web server) or should be discarded completely.
    5. If the file needs to be uploaded to the FTP server, it needs to be renamed with a special name.

    Based on the above, my suggestion is:

    1. When the user clicks “Next” on the step where he selects the file from his computer, you need to save the file immediately to a temporary location on your web server. You save the file to this temporary folder on your web server by doing something like:

      if(FileUpload1.HasFile) //user selected a file
      {
          try
          {
            //D:\temp is a temp directory on the Web Server
            FileUpload1.PostedFile.SaveAs(@"D:\temp\"+FileUpload1.FileName);
            //Store the FULL PATH TO the file just uploaded on Session 
            Session["FileName"]="D:\temp\"+FileUpload1.FileName;
          }
          catch (Exception ex)
          {
             //Handle it.
          }
      }
      
    2. On the last step of the wizard, assuming everything was successful, do this

       Dim myFtpWebRequest As FtpWebRequest
       Dim myFtpWebResponse As FtpWebResponse
       ' You know the NewFileName because it's the output of the wizard
      
       myFtpWebRequest = CType(WebRequest.Create(ftpServer + ftpPath + NewFilename),  FtpWebRequest)
       myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile
       myFtpWebRequest.UseBinary = True
      
       'Here you need to read the Original File
       Dim myFileStream As Stream = new FileStream(Session["FileName"]),FileMode.Open,FileAccess.Read,FileShare.ReadWrite)
       myFtpWebRequest.ContentLength = myFileStream.Length
      
      
       Dim requestStream As Stream = myFtpWebRequest.GetRequestStream()
       myFileStream.CopyTo(requestStream)
       requestStream.Close()
      
       myFtpWebResponse = CType(myFtpWebRequest.GetResponse(), FtpWebResponse)
       myFtpWebResponse.Close()
      
    3. If you decide that you should delete the original file uploaded by the user because he did not complete the wizard successfully, you can simply do:

       try
       {
          File.Delete (Session["FileName"]);
       }
       catch(Exception ex)
       {
          //Handle it.
       }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is related to my previous question about selecting visible elements . Now, here's
This is related to a previous question . What I'm trying to understand now
this is related to my previous question about jqgrid. im doing now a search
This is closely related to my previous question , which was about using CMake
This is related to my previous question I'm solving UVA's Edit Step Ladders and
This is related to my previous question More than 1 Left joins in MSAccess
This is related to my previous question here . I want 4 divs (absolute
This is related to my previous question , but a different one. I have
This is related to my previous question , regarding pulling objects from a dmp
This question is related to a previous question of mine That's my current code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.