Wednesday 3 February 2010

iron speed: upload a file to file system and save the location in database...solved

This is the first time when a product documentation has come to my rescue. The iron speed code documentation has done the trick for me. I am trying to figure out a way to provide the code snippet a format other than image so that it can be in a ‘copy and paste’ format for later purposes. This is the image of the product documentation that had helped me for this particular problem. Okey first thing’s first. How I got around the problem? As I have discussed in phase one that I want upload a file into the filesystem using the fileInput control. At the same time I wanted to save that virtual address to the DB location. My approach was I will designate the savelocation value to the url Textbox and ask iron speed to take data from the url Textbox. Another approach may be to declare a hidden asp label. Then assign the savelocation value to the label. Iron speed will pull the data from the label. A normal procedure could have been taking the value from the input file directly. The reason I did not take the approach is because InputFile is an html control. I did not want to get in another hassle of converting html variable into aspx data. Rest is fairly easy.

public void try1_Click(object sender, EventArgs args)             {                 bool shouldRedirect = true;                 try1_Click_Base(sender, args);                                  //searching the html control called inputFile                 System.Web.UI.HtmlControls.HtmlInputFile inputFile;                 inputFile = ((System.Web.UI.HtmlControls.HtmlInputFile)(this.Page.FindControl("inputFile")));                  if ((!(inputFile.PostedFile == null) && (inputFile.PostedFile.ContentLength > 0)))                 {                     // getting the filename                     // getting the savelocation                      string fileName = System.IO.Path.GetFileName(inputFile.PostedFile.FileName);                     string saveLocation = (this.Page.Server.MapPath("..\\Data") + ("\\" + fileName));                       try                      {                         // start transaction to store the file location to DB                         DbUtils.StartTransaction();                              // saving file to file system                         inputFile.PostedFile.SaveAs(saveLocation);                          // setting the control value  = saveLocation                         this.ImageRecordControl.url.Text = saveLocation;                          // saving data to DB                         this.ImageRecordControl.SaveData();                         this.CommitTransaction(sender);                      }                                                               catch (Exception ex)                     {                         this.Page.Response.Write(("Error: " + ex.Message));                         shouldRedirect = false;                         BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this, "BUTTON_CLICK_MESSAGE", ex.Message);                         this.RollBackTransaction(sender);                     }                      finally                     {                         DbUtils.EndTransaction();                     }                      if (shouldRedirect)                     {                         this.ShouldSaveControlsToSession = true;                     }                  }                 else                  {                     this.Page.Response.Write("Please select a file to upload.");                 }         }








No comments: