我有一个页面,其中有一个asp:fileupload
,然后当图片上传时,它将被存储到图像文件夹中。我还制作了另一个网页,它有一个gridview,可以从目标文件夹中获取图像。但是我的问题是我想在点击时下载上传的图像。我搜索过谷歌,但我的查询没有答案。希望您能够帮助我。这是我在网格视图部分的代码,它获取所有上传的图像:
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();
}
答案 0 :(得分:0)
public class MyNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform
{
#region INameTransform 成员
public string TransformDirectory(string name)
{
return null;
}
public string TransformFile(string name)
{
return Path.GetFileName(name);
}
#endregion
}
public partial class Default1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ZipFileByCode();
}
/// <summary>
/// 压缩打包文件
/// </summary>
public void ZipFileByCode()
{
MemoryStream ms = new MemoryStream();
byte[] buffer = null;
using (ZipFile file = ZipFile.Create(ms))
{
file.BeginUpdate();
file.NameTransform = new MyNameTransfom();//通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。
file.Add(Server.MapPath("/Content/images/img01.jpg"));
file.Add(Server.MapPath("/Content/images/img02.jpg"));
file.CommitUpdate();
buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length);
}
Response.AddHeader("content-disposition", "attachment;filename=test.zip");
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
}
}
您需要下载dll ICSharpCode.SharpZipLib
答案 1 :(得分:0)
您必须为此操作编写处理程序,并在ProcessRequest()
方法中编写用于下载文件的代码
//我已将ContentType
设置为"application/octet-stream"
,涵盖任何类型的文件
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();
使用上面的代码,您可以下载图像。
答案 2 :(得分:0)
有很多方法可以通过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;
}
}
单击链接按钮时将下载图像。它会像这样。