Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. problem with uploading/downloading .docx file

problem with uploading/downloading .docx file

Scheduled Pinned Locked Moved ASP.NET
csharpcomhelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shankbond
    wrote on last edited by
    #1

    Hi, I have a problem similar to this post: http://www.microsoft.com.nsatc.net/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.office.developer.vba&tid=6fc07fd9-3313-4c09-9457-f6971ecafda4&cat=en_US_9477b478-d551-48fe-b931-e0d7ed05ac44&lang=en&cr=US&sloc=&p=1 In this post the original poster has written his code in VB whereas I have written my code in C# My code for uploading a file:

    protected void Button1_Click(object sender, EventArgs e)
    {
    if (FileUpload1.PostedFile.ContentLength>0)
    {
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string storagepath = "D:\\Test\\" + filename;
    int contentLen = FileUpload1.PostedFile.ContentLength;
    Byte[] data = new Byte[contentLen];
    FileUpload1.PostedFile.InputStream.Read(data, 0, contentLen);
    savefiletodatabase(data);
    FileUpload1.SaveAs(storagepath);
    }
    }

    private void savefiletodatabase(byte[] data)
    {
    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    try
    {
    SqlCommand command = new SqlCommand("INSERT into dbo.test ([file], [filename], [path]) VALUES (@data, '" + FileName + "', '" + FilePath + "')", connection);
    command.Parameters.AddWithValue("data", data);
    connection.Open();
    command.Connection = connection;
    command.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally { connection.Close(); }
    }

    The code to retrieve file goes here:

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
    getfile();
    }

    private void getfile() 
    {
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings\["ConnectionString"\].ToString());
        try
        {
            SqlCommand command = new SqlCommand("Select \[file\] FROM dbo.test where id='" + Id + "'", connection);
            SqlDataReader reader;
            connection.Open();
            command.Connection = conn
    
    H 1 Reply Last reply
    0
    • S shankbond

      Hi, I have a problem similar to this post: http://www.microsoft.com.nsatc.net/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.office.developer.vba&tid=6fc07fd9-3313-4c09-9457-f6971ecafda4&cat=en_US_9477b478-d551-48fe-b931-e0d7ed05ac44&lang=en&cr=US&sloc=&p=1 In this post the original poster has written his code in VB whereas I have written my code in C# My code for uploading a file:

      protected void Button1_Click(object sender, EventArgs e)
      {
      if (FileUpload1.PostedFile.ContentLength>0)
      {
      string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
      string storagepath = "D:\\Test\\" + filename;
      int contentLen = FileUpload1.PostedFile.ContentLength;
      Byte[] data = new Byte[contentLen];
      FileUpload1.PostedFile.InputStream.Read(data, 0, contentLen);
      savefiletodatabase(data);
      FileUpload1.SaveAs(storagepath);
      }
      }

      private void savefiletodatabase(byte[] data)
      {
      SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
      try
      {
      SqlCommand command = new SqlCommand("INSERT into dbo.test ([file], [filename], [path]) VALUES (@data, '" + FileName + "', '" + FilePath + "')", connection);
      command.Parameters.AddWithValue("data", data);
      connection.Open();
      command.Connection = connection;
      command.ExecuteNonQuery();
      }
      catch (Exception ex)
      {
      throw ex;
      }
      finally { connection.Close(); }
      }

      The code to retrieve file goes here:

      protected void LinkButton1_Click(object sender, EventArgs e)
      {
      getfile();
      }

      private void getfile() 
      {
          SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings\["ConnectionString"\].ToString());
          try
          {
              SqlCommand command = new SqlCommand("Select \[file\] FROM dbo.test where id='" + Id + "'", connection);
              SqlDataReader reader;
              connection.Open();
              command.Connection = conn
      
      H Offline
      H Offline
      Hesham Amin
      wrote on last edited by
      #2

      shankbond wrote:

      also when I uploaded a file to the database it was of 18041 bytes, but when I retrieved it after saving on desktop the size was 19,756 bytes which is increased?

      I think you need to clear the response using Response.Clear() It's very important not to use string concatenation to make sql statements. Use parameterized queries instead.

      Hesham A. Amin My blog twitter: @HeshamAmin

      S 1 Reply Last reply
      0
      • H Hesham Amin

        shankbond wrote:

        also when I uploaded a file to the database it was of 18041 bytes, but when I retrieved it after saving on desktop the size was 19,756 bytes which is increased?

        I think you need to clear the response using Response.Clear() It's very important not to use string concatenation to make sql statements. Use parameterized queries instead.

        Hesham A. Amin My blog twitter: @HeshamAmin

        S Offline
        S Offline
        shankbond
        wrote on last edited by
        #3

        Hi,

        hspc wrote:

        I think you need to clear the response using Response.Clear()

        I did but it does no effect on the size.

        Thanks Shankbond

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups