您的位置:首页 > 其它

附件下载功能

2016-05-04 17:07 417 查看
1、下载链接

<a href='<%=Request.ApplicationPath%>/CommonPages/RetrieveImageData.aspx?TableName=Knowledge&FieldName=Upload&RowGuid=<%#Eval("RowGuid") %>'>
下载
</a>


2、下载功能调用页面:

RetrieveImageData.aspx

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RetrieveImageData.aspx.cs" Inherits="RongGuangSys.RongGuangTable.CommonPages.RetrieveImageData"   theme="" styleSheetTheme=""%>


3、后台功能实现

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CommonPages
{
public partial class RetrieveImageData : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
Response.Clear();
string TableName = Request.QueryString["TableName"];
string FieldName = Request.QueryString["FieldName"];
string RowGuid = Request.QueryString["RowGuid"];

MisGuidRow oRow = new MisGuidRow(TableName, RowGuid);

if (Request["TempFile"] != null)
{
string fileName = "";
#region 如果采用生成文件的方式,会在服务器的Web程序目录下面创建临时文件,
if (oRow.R_HasFilled && !Convert.IsDBNull(oRow[FieldName]))
{
if (oRow[FieldName + "_FileName"] != null)
{
fileName = oRow[FieldName + "_FileName"].ToString();
}
else
{
fileName = Guid.NewGuid().ToString();
}

//替换开始
string guid = System.Guid.NewGuid().ToString();

FileStream objFileStream;
BinaryWriter objBinaryWriter;
string attPath = Request.ApplicationPath + "/RongGuangTempFile/" + TableName;
if (!Directory.Exists(Server.MapPath(attPath)))
{
Directory.CreateDirectory(Server.MapPath(attPath));
}

string strFullFilePath = Server.MapPath(attPath + "/" + fileName);

//二进制文件写必须用binaryWrite,不需要制定encoding方式
if (!File.Exists(strFullFilePath))
{
objFileStream = new FileStream(strFullFilePath, FileMode.OpenOrCreate, FileAccess.Write);
objBinaryWriter = new BinaryWriter(objFileStream);
objBinaryWriter.Write((byte[])oRow[FieldName]);
objBinaryWriter.Close();
objFileStream.Close();
}
Response.Redirect(attPath + "/" + fileName);
}
#endregion
}
else
{

if (oRow.R_HasFilled && !Convert.IsDBNull(oRow[FieldName]))
{
//Response.Charset = "utf-8";
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//'解决中文乱码之关键

Response.ContentType = oRow[FieldName + "_ContentType"].ToString();
if (oRow[FieldName + "_ContentType"].ToString().IndexOf("image/") < 0 && oRow[FieldName + "_FileName"] != null)
{
Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(oRow[FieldName + "_FileName"].ToString(), System.Text.Encoding.UTF8));

}
Response.BinaryWrite((byte[])oRow[FieldName]);
Response.End();
}
}

}

}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: