This project has moved. For the latest updates, please go here.

Upload to Database

Nov 17, 2008 at 5:27 PM
I'm looking for some pointers on how to use the control and upload files to database, as well as how to handle other variables associated with the uplaod.

For example, I'd like to have a file name field and a file description field that users can enter, then browse for the file to upload. Submit would result in sending the name, description and file contents to database.
Nov 18, 2008 at 8:22 AM
Gettting the file into the database is totally up to you. This control takes care of the upload process, the final result is a file on the harddisk of your server.

You can override the FinishedFileUpload method in the WCF webservice. In that method you have access to the file, filename and any custom parameters. That's the place to put your own code to copy the file into a database for example.
Nov 18, 2008 at 3:57 PM
Thanks for the response mpost.

Getting the file into the database after uploading is not a problem. I can upload, then write from the server to the database and then delete the file.

What I don't understand is how to use the custom parameters. For example, suppose the database should contain an item for a file description that is input into a text box by the user uploading a file. Can that user input be passed via the FinishedFileUpload method and then inserted into the database as well? Sorry to be dense on this.
Nov 18, 2008 at 5:13 PM

No, you can't pass user input on the fly. The custom param is made for the following scenario:

You're editing a product in a product catalog. You're on page: Product.aspx?ID=1500.
The product ID is 1500.

Server side, you alter the InitParameters from the Silverlight Control that is also on the product-edit page. It may look like this:
InitParameters ="MaxFileSizeKB=1000,MaxUploads=2,FileFilter=,CustomParam=1500,DefaultColor=LightBlue" />
Notice the: CustomParam=1500 (it's the same as your ProductID)

Now you can alter the FinishedFileUpload method in the WCF project. There you have access to the parameter, which will be 1500 in this case. You can write your code to link the uploaded files to the product with ID 1500.

So basically you need to have an ID or pointer when you're loading your page with the Silveright control on it.

When for example in the above scenario I'm creating a new product, the CustomParam would be useless, because I don't have a ProductID yet. You can work around that by making it two steps, first enter the product info, get the database id and load the page with the Silveright control on it.
Nov 18, 2008 at 5:56 PM
Thanks. That info may put me on the right path.
Mar 10, 2009 at 6:06 PM

Hi, tanks for your great work. I'll want use the FileFilter parameter..but i don't understand how i can do this. So..i think this:

to set the file filter setting on "HttpUploader_SilverlightMultiFileUploadTestPage.aspx"  i add the page Load on the page:

<script runat="server">
         void Page_Load(Object sender, EventArgs e)
             Xaml1.InitParameters = "HttpUploader=true,MaxFileSizeKB=,MaxUploads=,FileFilter=|*.xls,CustomParam=,DefaultColor=Red";

is possible filter 2 or more type of files? how i do this? (For example is correct lik FileFilter=|*.xls,|*.doc ?)

the nex step is to get this parameters on .ashx i do this :

    /// <summary>
    /// Get the querystring parameters
    /// </summary>
    private void GetQueryStringParameters()
        _fileName = _httpContext.Request.QueryString["file"];
        _filter = _httpContext.Request.QueryString["filter"];
        _parameters = _httpContext.Request.QueryString["param"];
        _lastChunk = string.IsNullOrEmpty(_httpContext.Request.QueryString["last"]) ? true : bool.Parse(_httpContext.Request.QueryString["last"]);
        _firstChunk = string.IsNullOrEmpty(_httpContext.Request.QueryString["first"]) ? true : bool.Parse(_httpContext.Request.QueryString["first"]);
        _startByte = string.IsNullOrEmpty(_httpContext.Request.QueryString["offset"]) ? 0 : long.Parse(_httpContext.Request.QueryString["offset"]); ;

and in HttpFileUploader class i do this :

private void StartUpload()
            long dataToSend = _dataLength - _dataSent;
            bool isLastChunk = dataToSend <= ChunkSize;
            bool isFirstChunk = _dataSent == 0;

            UriBuilder httpHandlerUrlBuilder = new UriBuilder(UploadUrl);
            httpHandlerUrlBuilder.Query = string.Format("{5}file={0}&offset={1}&last={2}&first={3}&param={4}", &filter={6}", _file.FileName, _dataSent, isLastChunk, isFirstChunk, _initParams, _filter,string.IsNullOrEmpty(httpHandlerUrlBuilder.Query) ? "" : httpHandlerUrlBuilder.Query.Remove(0, 1) + "&");

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(httpHandlerUrlBuilder.Uri);
            webRequest.Method = "POST";
            webRequest.BeginGetRequestStream(new AsyncCallback(WriteToStreamCallback), webRequest);


but so it's not work..the filter is empty..i saw wath is my error? Tanks for your attention..

Nicola, sorry for my english..i'm hope you understand what i write..!

Mar 10, 2009 at 8:03 PM

Hi Nicola,

The FileFilter parameter value is not available in the webservice or ashx http handler.

The FileFilter is only used with the OpenFileDialog. When you configure the file filter right, you will for example only be able to select JPG files in the file dialog, or only XLS files, whatever you configure. FileFilter needs to be configured like this: FileFilter=Text Files (*.txt)|*.txt|All Files (*.*)|*.*

It's a combination of name|filter. So if you only need XLS files you can use: FileFilter=XLS Files|*.xls

Good luck.