FileUpload to database (doc, xls, pdf, picture, etc.)

Last post 07-04-2009 2:04 PM by msingareddy. 12 replies.

Sort Posts:

  • FileUpload to database (doc, xls, pdf, picture, etc.)

    07-02-2009, 5:57 PM
    • Member
      16 point Member
    • msingareddy
    • Member since 03-06-2009, 10:58 AM
    • Posts 32

     Hi,

    I need to upload any file (picture, doc, pdf, excel etc.)  to the database. In display mode, I need to display the file name with a hyperlink. When this hyperlink is clicked the file should be downloaded from the database and displayed (embedded in the browser) or promt for saving it. When in edit mode, both the link and file upload control should be displayed to enable replacing the original file or inserting a new file.

    I have looked at DBImage and FileUpload examples but they are not quite the same to what I'm looking for. The Dbimage fieldtemplate does this only for the images/pcitures. The FileUpload fieldtemplates upload the files to a central file system and not to the database.

     

  • Re: FileUpload to database (doc, xls, pdf, picture, etc.)

    07-03-2009, 4:30 AM
    • Star
      12,160 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,548
    • TrustedFriends-MVPs

     Hi Msingareddy, have a look at my file upload field template here: Dynamic Data: Part 3-FileUpload FieldTemplates

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: FileUpload to database (doc, xls, pdf, picture, etc.)

    07-03-2009, 5:14 PM
    • Member
      16 point Member
    • msingareddy
    • Member since 03-06-2009, 10:58 AM
    • Posts 32

     Hi Steve,

    I see that in your example, you are saving the files to the file system. I need something that saves the file to the database and retrieves it.

    Thanks

  • Re: FileUpload to database (doc, xls, pdf, picture, etc.)

    07-03-2009, 5:21 PM
    • Star
      8,672 point Star
    • hans_v
    • Member since 01-29-2007, 9:03 PM
    • Posts 1,499
  • Re: FileUpload to database (doc, xls, pdf, picture, etc.)

    07-03-2009, 5:39 PM
    • Star
      12,160 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,548
    • TrustedFriends-MVPs

     Well then you can use the DBImage to effect this.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
  • Re: FileUpload to database (doc, xls, pdf, picture, etc.)

    07-03-2009, 11:40 PM
    • Member
      16 point Member
    • msingareddy
    • Member since 03-06-2009, 10:58 AM
    • Posts 32

     Thanks Steve, I've already looked at this and I'm trying to adapt DbImage sample from the futures to accomplish what I want. I'm having trouble invoking my custom FileHandler.

    I have create a custom FeildTemplate called FileImage and added a custom HttpHandler called FileHandler to the App_code folder. But when I set the ImageUrl to "~/FileHandler.axd?Table=xxxx&Field=yyyy&pk=1.....", the processRequest() method never gets invoked. When I manually type in the URL as

    http://www.myapp.com/FileHandler.axd?Table=xxxx&Field=yyyy&pk=1....., I get a "Resouce not found" error.

    Can you please point what I'm doing wrong?

    Thanks

  • Re: FileUpload to database (doc, xls, pdf, picture, etc.)

    07-04-2009, 5:23 AM
    Answer
    • Star
      12,160 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,548
    • TrustedFriends-MVPs

     Hi Msingareddy, have you looked at Scott Hunter original article here: Sample for Displaying Images from the Database using Dynamic Data. I'm not sure how you have implemented your FileHandler.axd if it is not a phisical file then you may need to add a route i.e.:

    // Create a special escape route for handling resource requests. 
    // The this route will be evaluated for all requests but the // contraint will only match requests with WebResource.axd on the end. // The StopRoutingHandler will instruct the routing system to stop
    // processing this request and pass
    // it on to standard ASP.NET processing. routes.Add(new Route("{*resource}", new StopRoutingHandler()) { Constraints = new RouteValueDictionary(new { resource = @".*WebResource\.axd" }) });


    Like the one for the resource handler in the DD Futures project.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under: ,
  • Re: FileUpload to database (doc, xls, pdf, picture, etc.)

    07-04-2009, 12:13 PM
    • Member
      16 point Member
    • msingareddy
    • Member since 03-06-2009, 10:58 AM
    • Posts 32

    Thanks Steve, that was it. I registered the handler in web.config and I was able to invoke my FileHandler.axd code. However I'm running into another issue. I'm using EF and I'm running into the following error when using DbImage sample.

    // Items.Where(row => row.ID == 1).Single()
    
    var singleWhereCall = LinqExpressionHelper.BuildSingleItemQuery(query, metaTable, primaryKeyValues.ToArray());
    
    // Items.Where(row => row.ID == 1).Single().Picture
    var fileBytes = Expression.Property(singleWhereCall, column);
    
    object file = query.Provider.Execute(fileBytes);
                

    The error :

    The method 'Single' is not supported by LINQ to Entities. Consider using the method 'First' instead.

    How can I change the above LINQ query to adapt to Linq to Entities?

    Thanks

  • Re: FileUpload to database (doc, xls, pdf, picture, etc.)

    07-04-2009, 12:54 PM
    Answer
    • Star
      12,160 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,548
    • TrustedFriends-MVPs

     Yes that's a common issue going from L2S to EF EF does not support Single or SingleOrDefault you will need to change it to First or FirstOrDefault.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: FileUpload to database (doc, xls, pdf, picture, etc.)

    07-04-2009, 12:59 PM
    • Member
      16 point Member
    • msingareddy
    • Member since 03-06-2009, 10:58 AM
    • Posts 32

     Yes I need to use FirstOrDefault. Can you show me how I can modify the above code to use that?

  • Re: FileUpload to database (doc, xls, pdf, picture, etc.)

    07-04-2009, 1:12 PM
    Answer
    • Star
      12,160 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,548
    • TrustedFriends-MVPs

     Yes of course, you will need to go to the LinqExpressionHelp class and edit this method
    (in the Dynamic Data Futures VS2008 SP1 RTM project Jun 17 2008, Stable):

    public static MethodCallExpression BuildSingleItemQuery(IQueryable query, MetaTable metaTable, string[] primaryKeyValues)
    {
        // Items.Where(row => row.ID == 1)
        var whereCall = BuildItemsQuery(query, metaTable, metaTable.PrimaryKeyColumns, primaryKeyValues);
        // Items.Where(row => row.ID == 1).Single()
        var singleCall = Expression.Call(typeof(Queryable), "Single", new Type[] { metaTable.EntityType }, whereCall);
    
        return singleCall;
    }

    and change the Single to First.

    P.S. there could be others that need changing.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: FileUpload to database (doc, xls, pdf, picture, etc.)

    07-04-2009, 2:04 PM
    • Member
      16 point Member
    • msingareddy
    • Member since 03-06-2009, 10:58 AM
    • Posts 32

     Thanks Steve. that worked! Instead of making the change to the LinQExpressionHelp class I made the change directly in the handler class. Now I'm able to upload and download files from database.

    var whereCall = LinqExpressionHelper.BuildItemsQuery(query, 
    metaTable,cols,primaryKeyValues.ToArray());
    
    var singleWhereCall = Expression.Call(typeof(Queryable), "FirstOrDefault", new Type[] { metaTable.EntityType }, whereCall); 
    
    //var singleWhereCall = LinqExpressionHelper.BuildSingleItemQuery(query, metaTable, primaryKeyValues.ToArray());
    
    // Items.Where(row => row.ID == 1).Single().Picture
    var fileBytes = Expression.Property(singleWhereCall, column);


    I've marked your reply as answer.

Page 1 of 1 (13 items)