File upload/download from database. Download appends aspx page to end of file

Discussion in 'ASP .Net' started by Ryan Taylor, Nov 10, 2004.

  1. Ryan Taylor

    Ryan Taylor Guest

    Hello.

    I am trying to upload a file and save it in a Sql Server 2000 database. This
    seems to be working fine. However, when I download the file from SQL Server,
    it appears that the page that is downloading this file is being appended to
    the end of the file. So I am accidently modifiying every file that is placed
    in the database. What follows is my code for uploading and downloading a
    file, minus the sql code to insert/retrieve the data into/from the database.
    Does anyone see what might be wrong? Thanks in advance.

    // uploading a file
    HttpPostedFile file = btnAttachFile.PostedFile;
    string fileName = System.IO.Path.GetFileName(file.FileName);
    string mimeType = file.ContentType;
    int fileSize = file.ContentLength;
    byte[] fileData = new byte[fileSize];
    file.InputStream.Read(fileData, 0, fileSize);

    int questionId = Convert.ToInt32(Request.Params["qId"]);
    int sessionId = Convert.ToInt32(Request.Params["sId"]);
    string btnAttach = Request.Params["btnAttach"];

    if(QstnrSql.UpdateAttachment(authUser, questionId, sessionId, fileName,
    mimeType, ref fileData))
    {
    // success
    }

    // downloading a file
    byte[] buffer = null;
    string attachmentFileName = "";
    string attachmentMimeType = "";
    if(QstnrSql.DownloadAttachment(authUser, questionId, sessionId, out buffer,
    out attachmentFileName, out attachmentMimeType))
    {
    // Convert the content to application/pdf
    Response.Clear();
    Response.ContentType = attachmentMimeType;
    Response.AppendHeader("Content-Disposition", "Attachment; Filename=\"" +
    attachmentFileName +"\"");
    Response.BinaryWrite(buffer);
    Response.Flush();
    }


    // orginal file orginalfile.txt
    hello

    //downloaded file originalfile2.txt
    hello
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>ACMS: View Attachment</title>
    <LINK href="../AcmsStyles.css" type="text/css" rel="stylesheet">
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5"
    name="vs_targetSchema">
    </HEAD>
    <body leftMargin="0" topMargin="0" ms_positioning="FlowLayout"
    marginheight="0" marginwidth="0">

    <script language="JavaScript1.2"
    vqm_id="/acms/MenuScripts/ResOrgAdmin/ResOrgAdminData.js"
    type="text/javascript">

    vqm__notice='Visual QuickMenu Pro, (c) 2004 OpenCube Inc., All Rights
    Reserved, Visit - www.opencube.com';

    vqm__codebase='/acms/MenuScripts/ResOrgAdmin/';
    vqm__database='/acms/';

    </script>
    <script language="JavaScript1.2"
    src="/acms/MenuScripts/ResOrgAdmin/ResOrgAdminData.js"
    type="text/javascript"></script>
    <script language="JavaScript1.2"
    src="/acms/MenuScripts/ResOrgAdmin/tdqm_loader.js"
    type="text/javascript"></script>
    <div class="outterDiv">
    <table class="outterTable" cellSpacing="0" cellPadding="0">
    <tr>
    <td class="headerCell">
    <table id="tblHeader" cellspacing="0" cellpadding="0" class="headerTable">
    <tr>
    <td colspan="3" class="headerBarTop">
    </td>
    </tr>
    <tr>
    <td colspan="3" class="headerBarCenter"></td>
    </tr>
    <tr>
    <td colspan="3" class="headerBarBottom"></td>
    </tr>
    <tr>
    <td class="headerImgCell" rowspan="3">
    <img id="header_logo" class="headerLogoImg" src="../images/logo.jpg"
    alt="Logo" border="0" />
    </td>
    <td class="headerAppTitleCell" rowspan="3">
    <div class="headerAppTitle" id="appTitle">Acceleron Compliance Management
    System</div>
    </td>
    <td class="headerStatusCell">
    <span id="header_lblVersion" class="headerStatus">version: 0.4</span>
    </td>
    </tr>
    <tr>
    <td class="headerStatusCell">
    <span id="header_lblStatus" class="headerStatus">Logged in as
    RespondingAdmin<br>Responding Organization Administrator</span></td>
    </tr>
    <tr>
    <td class="headerStatusCell">
    <a id="header_lnkLogout" class="headerStatus"
    href="../logout.aspx">logout</a></td>
    </tr>
    <tr>
    <td colspan="3" class="headerBarTop"></td>
    </tr>
    <TR>
    <TD class="headerBarCenter" colSpan="3"></TD>
    </TR>
    <TR>
    <TD class="headerBarBottom" colSpan="3"></TD>
    </TR>
    </table>
    </td>
    </tr>
    <tr>
    <td class="innerTableCell">
    <table class="innerTable" cellSpacing="0" cellPadding="0">
    <tr>
    <td class="navbarCell">
    <script id="vqp_generate_mainitems"
    language="javascript1.2">generate_mainitems()</script></td>
    <td class="gutterCell"></td>
    <td class="contentCell">
    <form name="frmViewAttach" method="post"
    action="ViewAttach.aspx?sId=32&amp;qId=1128" id="frmViewAttach">
    <input type="hidden" name="__VIEWSTATE"
    value="dDwtMTY4MjY1MzA1Mjt0PDtsPGk8Mz47PjtsPHQ8O2w8aTwzPjtpPDU+Oz47bDx0PHA8c
    DxsPFRleHQ7PjtsPHZlcnNpb246IDAuNDs+Pjs+Ozs+O3Q8cDxwPGw8VGV4dDs+O2w8TG9nZ2VkI
    GluIGFzIFJlc3BvbmRpbmdBZG1pblw8YnJcPlJlc3BvbmRpbmcgT3JnYW5pemF0aW9uIEFkbWlua
    XN0cmF0b3I7Pj47Pjs7Pjs+Pjs+Pjs+k8oUXBcNrY4GyfjocrIX5+gh4O0=" />

    View Attachment
    </form>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    <tr>
    <td class="footerCell">
    Copyright &copy;2004, Acceleron Compliance Systems
    </td>
    </tr>
    </table>
    </div>
    </body>
    </HTML>
     
    Ryan Taylor, Nov 10, 2004
    #1
    1. Advertising

  2. Ryan Taylor

    bruce barker Guest

    add a Response.End() after the flush

    -- bruce (sqlwork.com)


    "Ryan Taylor" <> wrote in message
    news:e0mET$...
    | Hello.
    |
    | I am trying to upload a file and save it in a Sql Server 2000 database.
    This
    | seems to be working fine. However, when I download the file from SQL
    Server,
    | it appears that the page that is downloading this file is being appended
    to
    | the end of the file. So I am accidently modifiying every file that is
    placed
    | in the database. What follows is my code for uploading and downloading a
    | file, minus the sql code to insert/retrieve the data into/from the
    database.
    | Does anyone see what might be wrong? Thanks in advance.
    |
    | // uploading a file
    | HttpPostedFile file = btnAttachFile.PostedFile;
    | string fileName = System.IO.Path.GetFileName(file.FileName);
    | string mimeType = file.ContentType;
    | int fileSize = file.ContentLength;
    | byte[] fileData = new byte[fileSize];
    | file.InputStream.Read(fileData, 0, fileSize);
    |
    | int questionId = Convert.ToInt32(Request.Params["qId"]);
    | int sessionId = Convert.ToInt32(Request.Params["sId"]);
    | string btnAttach = Request.Params["btnAttach"];
    |
    | if(QstnrSql.UpdateAttachment(authUser, questionId, sessionId, fileName,
    | mimeType, ref fileData))
    | {
    | // success
    | }
    |
    | // downloading a file
    | byte[] buffer = null;
    | string attachmentFileName = "";
    | string attachmentMimeType = "";
    | if(QstnrSql.DownloadAttachment(authUser, questionId, sessionId, out
    buffer,
    | out attachmentFileName, out attachmentMimeType))
    | {
    | // Convert the content to application/pdf
    | Response.Clear();
    | Response.ContentType = attachmentMimeType;
    | Response.AppendHeader("Content-Disposition", "Attachment; Filename=\"" +
    | attachmentFileName +"\"");
    | Response.BinaryWrite(buffer);
    | Response.Flush();
    | }
    |
    |
    | // orginal file orginalfile.txt
    | hello
    |
    | //downloaded file originalfile2.txt
    | hello
    | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    | <HTML>
    | <HEAD>
    | <title>ACMS: View Attachment</title>
    | <LINK href="../AcmsStyles.css" type="text/css" rel="stylesheet">
    | <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    | <meta content="C#" name="CODE_LANGUAGE">
    | <meta content="JavaScript" name="vs_defaultClientScript">
    | <meta content="http://schemas.microsoft.com/intellisense/ie5"
    | name="vs_targetSchema">
    | </HEAD>
    | <body leftMargin="0" topMargin="0" ms_positioning="FlowLayout"
    | marginheight="0" marginwidth="0">
    |
    | <script language="JavaScript1.2"
    | vqm_id="/acms/MenuScripts/ResOrgAdmin/ResOrgAdminData.js"
    | type="text/javascript">
    |
    | vqm__notice='Visual QuickMenu Pro, (c) 2004 OpenCube Inc., All Rights
    | Reserved, Visit - www.opencube.com';
    |
    | vqm__codebase='/acms/MenuScripts/ResOrgAdmin/';
    | vqm__database='/acms/';
    |
    | </script>
    | <script language="JavaScript1.2"
    | src="/acms/MenuScripts/ResOrgAdmin/ResOrgAdminData.js"
    | type="text/javascript"></script>
    | <script language="JavaScript1.2"
    | src="/acms/MenuScripts/ResOrgAdmin/tdqm_loader.js"
    | type="text/javascript"></script>
    | <div class="outterDiv">
    | <table class="outterTable" cellSpacing="0" cellPadding="0">
    | <tr>
    | <td class="headerCell">
    | <table id="tblHeader" cellspacing="0" cellpadding="0" class="headerTable">
    | <tr>
    | <td colspan="3" class="headerBarTop">
    | </td>
    | </tr>
    | <tr>
    | <td colspan="3" class="headerBarCenter"></td>
    | </tr>
    | <tr>
    | <td colspan="3" class="headerBarBottom"></td>
    | </tr>
    | <tr>
    | <td class="headerImgCell" rowspan="3">
    | <img id="header_logo" class="headerLogoImg" src="../images/logo.jpg"
    | alt="Logo" border="0" />
    | </td>
    | <td class="headerAppTitleCell" rowspan="3">
    | <div class="headerAppTitle" id="appTitle">Acceleron Compliance
    Management
    | System</div>
    | </td>
    | <td class="headerStatusCell">
    | <span id="header_lblVersion" class="headerStatus">version: 0.4</span>
    | </td>
    | </tr>
    | <tr>
    | <td class="headerStatusCell">
    | <span id="header_lblStatus" class="headerStatus">Logged in as
    | RespondingAdmin<br>Responding Organization Administrator</span></td>
    | </tr>
    | <tr>
    | <td class="headerStatusCell">
    | <a id="header_lnkLogout" class="headerStatus"
    | href="../logout.aspx">logout</a></td>
    | </tr>
    | <tr>
    | <td colspan="3" class="headerBarTop"></td>
    | </tr>
    | <TR>
    | <TD class="headerBarCenter" colSpan="3"></TD>
    | </TR>
    | <TR>
    | <TD class="headerBarBottom" colSpan="3"></TD>
    | </TR>
    | </table>
    | </td>
    | </tr>
    | <tr>
    | <td class="innerTableCell">
    | <table class="innerTable" cellSpacing="0" cellPadding="0">
    | <tr>
    | <td class="navbarCell">
    | <script id="vqp_generate_mainitems"
    | language="javascript1.2">generate_mainitems()</script></td>
    | <td class="gutterCell"></td>
    | <td class="contentCell">
    | <form name="frmViewAttach" method="post"
    | action="ViewAttach.aspx?sId=32&amp;qId=1128" id="frmViewAttach">
    | <input type="hidden" name="__VIEWSTATE"
    |
    value="dDwtMTY4MjY1MzA1Mjt0PDtsPGk8Mz47PjtsPHQ8O2w8aTwzPjtpPDU+Oz47bDx0PHA8c
    |
    DxsPFRleHQ7PjtsPHZlcnNpb246IDAuNDs+Pjs+Ozs+O3Q8cDxwPGw8VGV4dDs+O2w8TG9nZ2VkI
    |
    GluIGFzIFJlc3BvbmRpbmdBZG1pblw8YnJcPlJlc3BvbmRpbmcgT3JnYW5pemF0aW9uIEFkbWlua
    | XN0cmF0b3I7Pj47Pjs7Pjs+Pjs+Pjs+k8oUXBcNrY4GyfjocrIX5+gh4O0=" />
    |
    | View Attachment
    | </form>
    | </td>
    | </tr>
    | </table>
    | </td>
    | </tr>
    | <tr>
    | <td class="footerCell">
    | Copyright &copy;2004, Acceleron Compliance Systems
    | </td>
    | </tr>
    | </table>
    | </div>
    | </body>
    | </HTML>
    |
    |
    |
     
    bruce barker, Nov 10, 2004
    #2
    1. Advertising

  3. Ryan Taylor

    Ryan Taylor Guest

    Awesome.
    Thanks Bruce.

    Ryan Taylor
     
    Ryan Taylor, Nov 10, 2004
    #3
    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. Sanjay Patil
    Replies:
    0
    Views:
    765
    Sanjay Patil
    May 10, 2004
  2. Brett  Kelly
    Replies:
    1
    Views:
    692
    Steve C. Orr [MVP, MCSD]
    Jun 16, 2006
  3. Bogdan
    Replies:
    0
    Views:
    1,728
    Bogdan
    Sep 18, 2009
  4. dmiller23462

    Email AND File Appends

    dmiller23462, May 27, 2004, in forum: ASP General
    Replies:
    1
    Views:
    126
    Me Padre
    May 28, 2004
  5. Guy
    Replies:
    5
    Views:
    191
    Walt Mankowski
    Dec 10, 2003
Loading...

Share This Page