附件下载功能

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();
                }
            }

        }


    }
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值