附件下载功能
2016-05-04 17:07
417 查看
1、下载链接
2、下载功能调用页面:
RetrieveImageData.aspx
前台:
3、后台功能实现
<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(); } } } } }
相关文章推荐
- Spring @Resource、@Autowired、@Qualifier的注解注入及区别
- Ajax复选框多选删除的实现
- nctf-php
- sql基础知识-----主外键、参照完整性
- 深入Spring
- hdu 1937(尺取法)
- centos 70 在vmware中安装笔记
- 内建模块-HTMLParser
- android 基础 @SuppressLint("NewApi")和@TargetApi()的作用与区别
- Shiro 如何主动调用doGetAuthorizationInfo方法
- Java加密技术(二)——对称加密算法DES&AES
- Qt Gui使用(二)
- 关于swift - 网络请求+MD5,混合编程
- Unreal Engine 4 —— Post Process Shader练手(第二弹)
- 字符串模式匹配中BF算法和KMP算法的java实现
- Unreal Engine 4 —— Post Process Shader练手(第二弹)
- 自定义QItemDelegate实现带CheckBox复选框的自增ID表格列
- 关于CollectionView的九宫格排布,多选,全选实现及删除
- 磁盘阵列恢复方法以及注意事项
- HTTP的简单理解