Response.BinaryWrite HELP!

Discussion in 'ASP .Net' started by Dave, Oct 27, 2008.

  1. Dave

    Dave Guest

    I have a small web app that publishes files to a SQL database. I just
    noticed this morning that 2 conditions exist that I need help with.

    1.) Any file that can be open with NotePad have the HTML for the web
    page appended to the document when it's received. Example...

    This line was entered in the *.txt file prior to upload. Everything
    below this was added when retrieving from SQL.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
    www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>
    Untitled Page
    </title>
    <link href="App_Themes/j


    End Example


    Upload Code:
    VaultGridView.SelectedIndex = -1;
    strConnection =
    ConfigurationManager.ConnectionStrings["DocumentVaultConnectionString"].ConnectionString;
    dbConn = new SqlConnection(strConnection);
    dbConn.Open();
    dcmd = new SqlCommand();
    dcmd.CommandText = "INSERT INTO [DocumentVault].[dbo].
    [tblVault] " +
    "([tID] " +
    ",[FileName] " +
    ",[Filesize] " +
    ",[FileData] " +
    ",[uBy] " +
    ",[uDate]) " +
    "VALUES " +
    "(@tID " +
    ",@FileName " +
    ",@Filesize " +
    ",@FileData " +
    ",@uBy " +
    ",@uDate)";
    dcmd.Parameters.Add(new SqlParameter("tID",
    TumblersGridView.SelectedValue));
    dcmd.Parameters.Add(new SqlParameter("Filename",
    FileUpload1.FileName));
    dcmd.Parameters.Add(new SqlParameter("Filesize",
    FileUpload1.PostedFile.ContentLength));
    dcmd.Parameters.Add(new SqlParameter("FileData",
    FileUpload1.FileBytes));
    dcmd.Parameters.Add(new SqlParameter("uBy",
    Session["UserID"].ToString()));
    dcmd.Parameters.Add(new SqlParameter("uDate",
    DateTime.Now.Date));
    dcmd.Connection = dbConn;
    dcmd.ExecuteNonQuery();
    VaultGridView.DataBind();




    Download Code:
    strConnection =
    ConfigurationManager.ConnectionStrings["DocumentVaultConnectionString"].ConnectionString;
    dbConn = new SqlConnection(strConnection);
    dbConn.Open();
    dcmd = new SqlCommand();
    dcmd.CommandText = "SELECT [FileName], [FileData] " +
    "FROM [DocumentVault].[dbo].[tblVault] " +
    "WHERE([vID] = @vID)";
    dcmd.Parameters.Add(new SqlParameter("vID",
    VaultGridView.SelectedValue));
    dcmd.Connection = dbConn;
    SqlDataReader dr = dcmd.ExecuteReader();
    dr.Read();

    Response.Clear();
    Response.ContentType = "application/x-unknown";
    Response.AppendHeader("Content-Disposition",
    "attachment; filename=\"" + dr["FileName"] +
    "\"");
    Response.BinaryWrite((byte[])dr["FileData"]);

    dbConn.Close();
    VaultGridView.SelectedIndex = -1;





    2.) All Office 2007 documents when retrieved open as corrupt.


    Everything that I've found so far shows that Response.WriteBinary is
    the right directrion. If it's not, I'm sure that there's someone out
    there that can give me the right path to take.

    Thanks
     
    Dave, Oct 27, 2008
    #1
    1. Advertising

  2. Dave wrote:
    > I have a small web app that publishes files to a SQL database. I just
    > noticed this morning that 2 conditions exist that I need help with.
    >
    > 1.) Any file that can be open with NotePad have the HTML for the web
    > page appended to the document when it's received. Example...
    >
    > This line was entered in the *.txt file prior to upload. Everything
    > below this was added when retrieving from SQL.
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
    > www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >
    > <html xmlns="http://www.w3.org/1999/xhtml">
    > <head><title>
    > Untitled Page
    > </title>
    > <link href="App_Themes/j
    >
    >
    > End Example
    >
    >
    > Upload Code:


    8< snip

    >
    > Download Code:


    8< snip

    >
    > 2.) All Office 2007 documents when retrieved open as corrupt.
    >
    >
    > Everything that I've found so far shows that Response.WriteBinary is
    > the right directrion. If it's not, I'm sure that there's someone out
    > there that can give me the right path to take.
    >
    > Thanks


    Remove all markup code from the aspx file, except the @Page directive.

    Add the property Theme="" to the @Page directive, so that it doesn't try
    to add theming to the page.

    Alternatively, if you really want the page to either work as a web page
    or a download proxy depending on some input, you can use Response.End()
    after your code that is writing the file to the response stream to keep
    the page from being rendeded to the stream also.

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    Göran Andersson, Oct 27, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DAC
    Replies:
    1
    Views:
    2,037
    Steve C. Orr, MCSD
    Jul 31, 2003
  2. =?Utf-8?B?QnJhZCBSb2JlcnRz?=

    RE: Response.BinaryWrite or something causes multiple posts

    =?Utf-8?B?QnJhZCBSb2JlcnRz?=, Aug 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    570
    =?Utf-8?B?RGFuIEM=?=
    Aug 19, 2004
  3. bruce barker
    Replies:
    2
    Views:
    644
    bruce barker
    Aug 19, 2004
  4. Replies:
    1
    Views:
    845
  5. Dave

    Response.BinaryWrite Help

    Dave, Oct 27, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    336
    Anthony Jones
    Oct 27, 2008
Loading...

Share This Page