Forum
smartDBforms.NET for ASP.NET 2
 
 
Knowledge Base Forum
To post messages on the forum, you need to login first.
SearchForum Home
  Product Forums  smartDBforms.NET  File Upload...
 File Upload
 
 5/11/2010 11:02:19 PM
Brett
4 posts


File Upload

I'm messing with the fileupload control.  I'm normally a big advocate for the "store it in the filesystem and keep a link in the database", but I was working on a "quick and dirty" and was going to just store it in the DB.

So, the upload works fine and binary data is in the database, but how can I capture the filename to store as well to display to the user later without them having to type it into a text box (they entered the upload filename, I'm sure there is some place I can override and use that to insert as the filename?).

 

Also, since I'm a SQL Server guy, is there support for 2008s filestream objects?

Thanks

 5/13/2010 7:41:41 AM
Adillis Support
43 posts
adillis.com


Re: File Upload

Hi Brett,

Currently there is a template which can handle the strategy "store it in the filesystem and keep a link in the database". It is named FileNameFromDiskTemplate. It works with text fields. It stores the name of the file(without the path) in the db field and the file itself in a predefined subfolder of the web project. See below for a full explanation.

I am  currently analyzing your request for capturing the file name of uploaded binary data and setting the name into the corresponding text template. It will require some modification in the smartDBtemplates/TemplatesStorage.ascx file in order to notify when new file is uploaded. I will write back, when an example implementation is ready.

The MS Sql 2008 FILESTREAM columns are supported. They behave in the same way as the normal varbinary columns. They are represented in .NET as byte array and smartDBforms.NET uses the upload control to edit the column content.

Here is more information on FileNameFromDiskTemplate from smartDBforms.NET User's Guide - smartDBtemplates Template Set:

FileNameFromDiskTemplate - Handles columns which are of type string, where the value is a name of a file. The file is stored separately on the file system. The template is not included in the template selection method. You can use explicit template selection by setting the TemplateID of SmartDBControl.

The folder where the files reside is specified in the appSettings section in Web.config. The setting is named SDBF_UploadDirectory and must point to a virtual directory. For example "~/UploadDir/". See the installed DemoSite Web Project.

In ReadOnly mode the FileNameFromDiskTemplate template displays either a link to download the file or an image depending on the extension of the actual file. In Edit mode the user can upload a new file. The file is saved in the designated folder and only the file name is stored in the database. If the database column has text length limitation then the file name is shortened( preserving the extension ) in order to fit into the specified limit.

You can set a maximum permitted size of the uploaded files for the ImageFromDBTemplate, FileFromDBTemplate and FileNameFromDiskTemplate templates in two ways:

  • To set the maximum permitted file size use the SDBF_UploadMaxFileBytes application setting. It should be added in the appSettings section of Web.config file. It will affect all database forms in the website. For example see the smartDBforms.NET Demo website.
  • To set the maximum permitted file size only for a specific SmartDBControl use the MaximumValue adhoc property in the SmartDBControl.AdHocProperties collection.

The value of SDBF_UploadMaxFileBytes and MaximumValue properties should be in bytes. For example to set maximum 4 MB you should put 4194304 as value. 4 MB = 4*1024*1024 = 4194304.
If nothing is specified, then by default the maximum permitted upload file size is 100 K.

smartDBforms.NET User's Guide - smartDBtemplates Template Set


If you have any other questions, we will be more than happy to help you.
Best wishes,
Adillis Support
Adillis - IT solutions http://adillis.com
 5/17/2010 10:28:34 AM
Adillis Support
43 posts
adillis.com


Re: File Upload

Hi Brett,

About your request for automatically populating a text field with the name of the file, uploaded to file/image field.
In order to get the functionality 2 lines of code should be added to the smartDBforms.NET templates file. In this way an event will be fired when a new file is uploaded.

Below you can find a step-by-step guide how to do it.

I prepared a sample web project form. It uses  the MSSQL 2008 sample AdventureWorks2008 database. The sample edits the ProductPhoto database table.
The url of the sample zip file is http://adillis.com/Portals/0/ForumUploads/FileUploadNameSampleWebSite.zip.
You can copy the already modified smartDBtemplates\TemplatesStorage.ascx file directly from it.
In the Default.aspx file you can see the CustomEvent handler(ctrlThumbNailPhoto_CustomEvent) implementation which receives the filename and sets it to the corresponding SmartDBControl(ctrlThumbnailPhotoFileName).
When you upload a new file to the ThumbNailPhoto field the ThumbnailPhotoFileName text automatically changes.

  1. Open smartDBtemplates\TemplatesStorage.ascx file.
  2. Add the following code to the end of the two functions - btnUploadDBImage_Click and btnUploadDBFile_Click:
    // The new file was uploaded. Send its filename to the hosting page.
    this.SendCustomEventToContainer(btnUpload, new ValidateCustomEvent(fileUpload.FileName));
    


    In  this way when a new file is uploaded in the FileFromDB and ImageFromDB templates a CustomEvent containing the filename will be fired to the hosting page.
  3. Open your smartDBforms.NET WebForm  where you want to propagate the newly uploaded file name to the text field.
  4. Create event handler for the CustomEvent event of the SmartDBControl which represents the FILE column.
  5. In the event handler add code to get the file name and set it to the value of the textbox, that is located in the text field SmartDBControl.
    For example:
    protected void ctrlThumbNailPhoto_CustomEvent(BaseSmartDBControl sender, BaseSmartDBControlCustomEventArgs args)
    {
        // Get the file name that was send by the upload template.
        ValidateCustomEvent ev = args.Event as ValidateCustomEvent;
        if (ev == null) return;
        string fileName = ev.Value as string;
    
        // Find the textbox for editing the ThumbnailPhotoFileName db column and set its text to the just uploaded filename.
        TextBox TextBoxValueString = ctrlThumbnailPhotoFileName.FindControl("TextBoxValueString") as TextBox;
        if (TextBoxValueString != null)  TextBoxValueString.Text = fileName;
    }
    


If you have any other questions, we will be more than happy to help you.
Best wishes,
Adillis Support
Adillis - IT solutions http://adillis.com
  Product Forums  smartDBforms.NET  File Upload...
Copyright 2006-2010 Adillis Terms Of Use Feedback Contact Us