Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何下载已上载的图像?_C#_Asp.net_Image_Download - Fatal编程技术网

C# 如何下载已上载的图像?

C# 如何下载已上载的图像?,c#,asp.net,image,download,C#,Asp.net,Image,Download,我有一个页面,其中有一个asp:fileupload,当图片上传时,它将被存储到一个图像文件夹中。我还制作了另一个网页,它有一个gridview,可以从目标文件夹中获取图像。然而,我的问题是,我想下载上传的图像时点击。我搜索过谷歌,但我的问题没有答案。希望你能帮助我。以下是我在grid view部分的代码,它从中获取所有上传的图像: protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPos

我有一个页面,其中有一个
asp:fileupload
,当图片上传时,它将被存储到一个图像文件夹中。我还制作了另一个网页,它有一个gridview,可以从目标文件夹中获取图像。然而,我的问题是,我想下载上传的图像时点击。我搜索过谷歌,但我的问题没有答案。希望你能帮助我。以下是我在grid view部分的代码,它从中获取所有上传的图像:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            binddepositslipgrid();

        }
    }
    public void binddepositslipgrid()
    {
        SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
        SqlCommand cmd = new SqlCommand("select DepositID, Image from DepositSlip ", conn);

        SqlDataAdapter da = new SqlDataAdapter("", conn);
        da.SelectCommand = new SqlCommand("select DepositID, Image from DepositSlip", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "data");
        gvDeposit.DataSource = ds.Tables[0].DefaultView;
        gvDeposit.DataBind();


    }
    protected void gvDeposit_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        int catid = int.Parse(gvDeposit.DataKeys[e.RowIndex].Value.ToString());
        SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
        SqlDataAdapter da = new SqlDataAdapter("", conn);
        conn.Open();
        da.DeleteCommand = new SqlCommand("delete from DepositSlip where DepositID=" + catid, conn);
        da.DeleteCommand.ExecuteNonQuery();
        conn.Close();
        binddepositslipgrid();
    }
公共类MyNameTransfom:ICSharpCode.SharpZipLib.Core.INameTransform
{
#大城市区域成员
公共字符串目录(字符串名称)
{
返回null;
}
公共字符串文件(字符串名称)
{
返回Path.GetFileName(名称);
}
#端区
}
公共部分类Default1:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
ZipFileByCode();
}
/// 
/// 压缩打包文件
/// 
公共无效ZipFileByCode()
{
MemoryStream ms=新的MemoryStream();
字节[]缓冲区=空;
使用(ZipFile file=ZipFile.Create(ms))
{
BeginUpdate()文件;
file.NameTransform=new MyNameTransfom()//通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在拉链中创建有关的文件夹。
Add(Server.MapPath(“/Content/images/img01.jpg”);
Add(Server.MapPath(“/Content/images/img02.jpg”);
file.CommitUpdate();
缓冲区=新字节[ms.Length];
ms.Position=0;
ms.Read(缓冲区,0,缓冲区长度);
}
AddHeader(“内容处置”、“附件;文件名=test.zip”);
二进制写入(缓冲区);
Response.Flush();
Response.End();
}
}

您需要下载dll
ICSharpCode.SharpZipLib

您必须为此操作编写处理程序,并在
ProcessRequest()
方法中编写下载文件的代码

//我已将
ContentType
设置为
“应用程序/八位字节流”
,它涵盖任何类型的文件

context.Response.ContentType = "application/octet-stream";

                    // context.Response.AddHeader("content-disposition", "attachment;filename=abc.jpg" + Path.GetFileName(file));
                    context.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);

                    //context.Response.w(file);
                    context.Response.OutputStream.Write(data, 0, data.Length);

                    //you can also implement other business request such as delete the file after download
                    context.Response.End();

使用上面的代码,您可以下载图像。

我在gridview中使用了链接按钮,有很多方法可以做到这一点。这是我的密码

   protected void gvDetails_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        try
        {
            GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
            Int32 RowIndex = row.RowIndex;
            DataTable dtable = new DataTable();
            dynamic di = new DirectoryInfo(FinalPath+"\\");
            dtable.Columns.Add("DownloadLink", typeof(string));
            dtable.Columns.Add("FileName", typeof(string));
            DataRow dr = null;
            foreach (FileInfo fi in di.GetFiles())
            {
                dr = dtable.NewRow();
                dr["FileName"] = fi.Name;
                dr["DownloadLink"] = FinalPath+"\\" + fi.Name;
                dtable.Rows.Add(dr);
            }
            int i = row.RowIndex;
            string filename = null;
            filename = dtable.Rows[i]["FileName"].ToString();
            string path__1 = (FinalPath + filename);
            string name = dtable.Rows[i]["FileName"] .ToString();
            string last = name.Substring(name.LastIndexOf('.'));
            string ext = last;
            string type = "";
            if (ext != null)
            {
                switch (ext.ToLower())
                {
                    case ".html":
                        {
                            type = "text/HTML";
                            break;
                        }

                    case ".txt":
                        {
                            type = "text/plain";
                            break;
                        }

                    case ".GIF":
                        {
                            type = "image/GIF";
                            break;
                        }

                    case ".pdf":
                        {
                            type = "Application/pdf";
                            break;
                        }

                    case ".doc":
                        {
                            type = "Application/doc";
                            break;
                        }
                    case ".rtf":
                        {
                            type = "Application/msword";
                            break; 
                        }

                    default:
                        {
                            type = "";
                            break; 
                        }

                }
            }
            Response.AppendHeader("content-disposition", "attachment; filename=" + name);
            if (!string.IsNullOrEmpty(type))
            {
                Response.ContentType = type;
            }
            Response.WriteFile(path__1);
            Response.End();
        }
        catch
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "myscript", "alert('Something Went Wrong!');", true);
            return;
        }
}
单击链接按钮时,图像将下载。看起来是这样的


因为图像的MIME类型可以用IIS来解释,所以我认为您可能需要将图像文件添加到zip文件中,然后下载zip文件,当然,这些操作必须自动完成。我如何才能做到这一点。此文件allready包含如下所示的ProcessRequest()public void ProcessRequest(HttpContext上下文)如何获取处理程序?无法搜索有关此的教程。对不起,我是新来的@Kiran Bhagat右键单击项目并添加新项目选择通用处理程序
   protected void gvDetails_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        try
        {
            GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
            Int32 RowIndex = row.RowIndex;
            DataTable dtable = new DataTable();
            dynamic di = new DirectoryInfo(FinalPath+"\\");
            dtable.Columns.Add("DownloadLink", typeof(string));
            dtable.Columns.Add("FileName", typeof(string));
            DataRow dr = null;
            foreach (FileInfo fi in di.GetFiles())
            {
                dr = dtable.NewRow();
                dr["FileName"] = fi.Name;
                dr["DownloadLink"] = FinalPath+"\\" + fi.Name;
                dtable.Rows.Add(dr);
            }
            int i = row.RowIndex;
            string filename = null;
            filename = dtable.Rows[i]["FileName"].ToString();
            string path__1 = (FinalPath + filename);
            string name = dtable.Rows[i]["FileName"] .ToString();
            string last = name.Substring(name.LastIndexOf('.'));
            string ext = last;
            string type = "";
            if (ext != null)
            {
                switch (ext.ToLower())
                {
                    case ".html":
                        {
                            type = "text/HTML";
                            break;
                        }

                    case ".txt":
                        {
                            type = "text/plain";
                            break;
                        }

                    case ".GIF":
                        {
                            type = "image/GIF";
                            break;
                        }

                    case ".pdf":
                        {
                            type = "Application/pdf";
                            break;
                        }

                    case ".doc":
                        {
                            type = "Application/doc";
                            break;
                        }
                    case ".rtf":
                        {
                            type = "Application/msword";
                            break; 
                        }

                    default:
                        {
                            type = "";
                            break; 
                        }

                }
            }
            Response.AppendHeader("content-disposition", "attachment; filename=" + name);
            if (!string.IsNullOrEmpty(type))
            {
                Response.ContentType = type;
            }
            Response.WriteFile(path__1);
            Response.End();
        }
        catch
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "myscript", "alert('Something Went Wrong!');", true);
            return;
        }
}