What control doI use to view uploaded files?

6 posts, 0 answers
  1. Garth
    Garth avatar
    9 posts
    Member since:
    Mar 2008

    Posted 24 Sep 2010 Link to this post

    What control doI use to view uploaded files after they are uploaded?

    For example:  I'll upload Word, Excel, PDF, PNG, TIFF, etc. files that will be stored into a varbinary(MAX) column.

    What control do I use in an SL app to view the stored files?
  2. Fletcher Aluminium
    Fletcher Aluminium avatar
    18 posts
    Member since:
    Jun 2010

    Posted 26 Sep 2010 Link to this post

    Hi Garth,

    I do not believe there a control which will allow you to view the files in the actual SL application.  However, how I am currently handling my file upload and then view, is to store the files in the database, and then display them in a generic List Box control (you can customize the template to include things like deletion and anything else you want).  Then I am handling the click event of the List Box control, and then invoke a Javascript function on the ASPX container web page which accepts a FileID which is the unique identifier of your file you uploaded, and then it redirects you to a new Web Page (which I have called DocumentViewer.aspx) which then handles the file and displays it.  And example of this is here.

    protected void Page_Load(object sender, EventArgs e)
    {
        string ID= Request["id"];
     
        Entities qie = new Entities();
         
        FileStore file = qie.FileStores.Where(a => a.FileID == id).SingleOrDefault();
     
        this.Title = file.Name;
     
        Response.Clear();
        Response.BufferOutput = true;
        Response.ContentType = file.MIMEType;
        if (!file.MIMEType.ToLower().Contains("image"))
        {
            Response.AddHeader("Content-Disposition", "attachment; filename=" +
            file.Name + file.Extension);
        }
        Response.BinaryWrite(file.FileData.ToArray());
        Response.End();
    }

    So this pretty much says, that any image MIMEType will show in the new browser window that opens up, or else it will prompt the user to either Open or Save the file which they clicked on.  This way, you can put any type of file in there, be it PDF, Image files, Word Docs, or even saved Email Messages.  However, if you want to, you could make your Silverlight app an out of browser application, and then using the WebBrowser control, display the images or files directly into the Silverlight App.

    Hope this has helped at all,


    Shaun
  3. DevCraft banner
  4. Garth
    Garth avatar
    9 posts
    Member since:
    Mar 2008

    Posted 27 Sep 2010 Link to this post

    Thanks, Shaun.  Great answer!
  5. Fletcher Aluminium
    Fletcher Aluminium avatar
    18 posts
    Member since:
    Jun 2010

    Posted 27 Sep 2010 Link to this post

    Hey Garth,

    No problem.  I hope it helped point you in the right direction, let me know if you had any great epiphanies as to how to make it that much better.

    Cheers,


    Shaun
  6. Swapnadip
    Swapnadip avatar
    7 posts
    Member since:
    Apr 2012

    Posted 23 Apr 2012 Link to this post

    Thanks shaun its a great answer.. But is it possible to do this in MVC3 RAZOR...and i want to download the attachments also.. how can i do it. i'm trying this since 2 days but unable to do that
  7. Fletcher Aluminium
    Fletcher Aluminium avatar
    18 posts
    Member since:
    Jun 2010

    Posted 23 Apr 2012 Link to this post

    Hey Swapnadip,

    I haven't really touched much on MVC3 yet.  I have only just started doing some work with it just recently. However, it also depends on the type of attachments/files that you are wanting to display on your MVC3 Website.  For example, for a PDF you should be able to use something like PDF in Partial View.  And then on that page, or below it or something have a download button which will allow the user to download it.  A quick good search of downloading files off a MVC3 Website should give you some good results. For example:
    public ActionResult Download()
    {
        var document = ...
        var cd = new System.Net.Mime.ContentDisposition
        {
            // for example foo.bak
            FileName = document.FileName,
     
            // always prompt the user for downloading, set to true if you want
            // the browser to try to show the file inline
            Inline = false,
        };
        Response.AppendHeader("Content-Disposition", cd.ToString());
        return File(document.Data, document.ContentType);
    }
    Then just store the file path of the document they are viewing and create a document object where it says var document = ... and you should be able to do what you want.

    However, in ASP.Net i know putting:
    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name + file.Extension);

    Before you display the page, will cause it to pop-up with a Save, Run, Cancel dialog box.

    Hope this helps.
Back to Top
DevCraft banner