2013-11-25 36 views
2

我开发的应用程序,一个Web应用程序,以及一个C#,文件上传在Web应用程序中完成的,这里是我的代码:下载文件c#

protected void ImageButton2_Click(object sender, ImageClickEventArgs e) // upload documents to database 
    { 
     foreach (HttpPostedFile upFile in FileUpload1.PostedFiles) 
     { 

      // Read the file and convert it to Byte Array 
      string filename = Path.GetFileName(upFile.FileName); 
      string ext = Path.GetExtension(filename); 
      string contenttype = String.Empty; 


      //Set the contenttype based on File Extension 
      switch (ext) 
      { 
       case ".doc": 
        contenttype = "application/vnd.ms-word"; 
        break; 
       case ".docx": 
        contenttype = "application/vnd.ms-word"; 
        break; 
       case ".xls": 
        contenttype = "application/vnd.ms-excel"; 
        break; 
       case ".xlsx": 
        contenttype = "application/vnd.ms-excel"; 
        break; 
       case ".jpg": 
        contenttype = "image/jpg"; 
        break; 
       case ".png": 
        contenttype = "image/png"; 
        break; 
       case ".gif": 
        contenttype = "image/gif"; 
        break; 
       case ".pdf": 
        contenttype = "application/pdf"; 
        break; 
       case ".rar": 
        contenttype = "application/rar"; 
        break; 
      } 

      if (contenttype != String.Empty) 
      { 

       Stream fs = FileUpload1.PostedFile.InputStream; 
       BinaryReader br = new BinaryReader(fs); 
       Byte[] bytes = br.ReadBytes((Int32)fs.Length); 

       //insert the file into database 
       string strQuery = "insert into UploadFile(referencenumber, Name, ContentType, Data, id)" + 
        " values (@referencenumber, @Name, @ContentType, @Data, @id)"; 
       SqlCommand cmd = new SqlCommand(strQuery); 
       cmd.Parameters.Add("@referencenumber", SqlDbType.VarChar).Value = disprefno.Text; 
       cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = 1; 
       cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename; 
       cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value 
        = contenttype; 
       cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes; 
       InsertUpdateData(cmd); 
      } 
     } 
    } 
    private Boolean InsertUpdateData(SqlCommand cmd) 
    { 
     //upload class 
     String strConnString = System.Configuration.ConfigurationManager 
     .ConnectionStrings["AIConnectionString"].ConnectionString; 
     SqlConnection con = new SqlConnection(strConnString); 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection = con; 
     try 
     { 
      lblUploadStat.Visible = true; 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
      return true; 
     } 
     catch (Exception ex) 
     { 
      Response.Write(ex.Message); 
      return false; 
     } 
     finally 
     { 
      string myStringVariable1 = string.Empty; 
      myStringVariable1 = "Documents Was Successfully Sent "; 
      ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable1 + "');", true); 
      lblUploadStat.Visible = false; 
      con.Close(); 
      con.Dispose(); 
     } 
    } 

现在,我希望通过从SQL数据库我的C#应用​​程序来获取上传文件,我怎么能做到这一点

+1

什么了你试试?我很难想象你做了所有这些,你不能把SELECT语句放到一个简单的C#应用​​程序中。 – nvoigt

+0

附注:我在使用'application/vnd.ms-excel'来处理扩展名为* .xlsx'的文件时遇到了问题;我现在使用的是application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'的内容类型(和'.docx'同上,我使用'application/vnd.openxmlformats-officedocument.wordprocessingml.document') –

回答

2

正好有我的想法,我想补充你的代码的一个非常,非常简单的版本:

using (SqlCommand cmd = new SqlCommand("select ContentType from UploadFile WHERE Id = 1")) 
{ 
    con.Open(); 
    SqlDataReader obj = cmd.ExecuteReader(); 
    byte[] bytes = (byte [])obj.GetValue(0); 
}