本文介绍了通过asp.net Web应用程序2005打开/下载文件(sql 2005二进制字段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.net Web应用程序2005

我有sql2005二进制字段,并存储在.doc,.pdf,.xls .... files

我想打开/下载这些文件.

给我一个整体的例子.

谢谢您

Hi i am using asp.net web application 2005

i have sql2005 binary field and stored in .doc, .pdf, .xls....files

i would like to open/download those files.

give me a total example.

Thanking you

推荐答案


public DownloadContent(string ID)
{
  SqlConnection con;
   try
        {

                con = new SqlConnection();
                con.ConnectionString = "YourConn";
                con.Open();
    
    
            string query = "your query to retrieve from db where ID = ID"// assuming 1 record here";
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader dr = cmd.ExecuteReader();
            
            if (dr.Read())
            {
                byte[] imagecontent = (byte[])(dr[1]); //assuming second record on dr is the file bytes
                Response.ContentType = "Application/msword"//sent your content type here, see ref link
                Response.AddHeader("Content-Disposition", "attachment;filename=somename.doc");//set file type here.
                Context.Response.BinaryWrite(imagecontent);
    
    
            }
            cmd = null;
            dr.Close();
            con.Close();
        }
        catch (Exception ex)
        {
        }
       finally
       {
        if(con!= null)
         con.Close();
       }
}


try
        {
            DataSet ds1 = new DataSet();
            GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);


            //ReportID = Convert.ToInt32(gvAssesReport.DataKeys[gvr.RowIndex].Values[1]);
            FileName = gvReport.DataKeys[gvr.RowIndex].Values[0].ToString();
            int ReportID = Convert.ToInt16(gvReport.DataKeys[gvr.RowIndex].Values[1].ToString());
            if (string.IsNullOrEmpty(FileName))
            {
                lblMessage.Text = "File not found";
            }
            else
            {
                if (File.Exists(Server.MapPath(Constants.Report_SavePath.ToString() + "//" + ReportID + "//" + FileName)))
                {
                    Response.Clear();
                    Response.AppendHeader("content-disposition", "attachment; filename=" + FileName);
                    Response.ContentType = "application/Ms-Word";
                    lblMessage.Text = string.Empty;
                    Response.TransmitFile(Server.MapPath(Constants.Report_DisplayPath.ToString() + "//" + ReportID + "//" + FileName));
                    Response.Flush();
                }
                else
                {
                    lblMessage.Text = "File not found";
                }
            }
        }
        catch (Exception ex)
        {


        }
        finally
        {
            Response.End();
        }


这篇关于通过asp.net Web应用程序2005打开/下载文件(sql 2005二进制字段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 23:40