asp.net 下载任意格式文件 上传文件后台代码
思路:将文件转化为流,输出到页面上的iframe中去
//下载附件逻辑
object DownLoad(NameValueCollection nv)
{
int attachId = nv["attachid"].ToInt();
SmalLessonAttach attachment = SmalLessonAttach.Get(attachId);
if (attachment == null)
{
return new {code=-1,msg="附件不存在!"};
}
string fileName = attachment.Name;
string filePath = Path.Combine(FileUtil.FormatDirectory(ConfigBase.GetConfig("doc")["file_root"]), attachment.Path, attachment.AttachmentId, attachment.Name);
if (!File.Exists(filePath))
return new {code = -2, msg = "附件不存在!"};
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
jc.Context.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
jc.Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
jc.Context.Response.BinaryWrite(bytes);
jc.Context.Response.Flush();
jc.Context.Response.End();
return new {code=1};
}
//上传附件逻辑
int UploadAttachment(NameValueCollection nv)
{
//微课不存在返回
int playid = nv["playid"].ToInt();
if (SmalLesson.Get(playid) == null) return -3;
//从这里开始保存附件
var files = httpContext.Request.Files;
if (files.Count == 0) return -1;
var file = files[0];
if (file.ContentLength == 0) return -2;
string root = ConfigBase.GetConfig("doc")["file_root"];
string dir = string.Format("Upload/weike/{0}", DateTime.Now.ToString("yy/MM/dd"));
string weiId = StringUtil.UniqueId();
string savePath = Path.Combine(root, dir, weiId);
if (!Directory.Exists(savePath))
Directory.CreateDirectory(savePath);
string fileName = Path.Combine(savePath, file.FileName);
file.SaveAs(fileName);
ILinqContext<SmalLessonAttach> cx = SmalLessonAttach.CreateContext(false);
SmalLessonAttach attachment = new SmalLessonAttach();
attachment.PlayId = playid;
attachment.Name = file.FileName;
attachment.UserId = LoginUser.FGuid;
attachment.Doctype = Path.GetExtension(file.FileName);
attachment.DateCreated = DateTime.Now;
attachment.AttachmentId = weiId;
attachment.Path = dir;
attachment.SchoolId = LoginUser.Schoolid;
cx.Add(attachment, true);
cx.SubmitChanges();
return 1;
}
var file = new FileInfo(filePath); //得到文件
if (file.Exists) //判断文件是否存在
{
jc.Context.Response.Clear(); //清空Response对象
/*设置浏览器请求头信息*/
jc.Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); //指定文件
jc.Context.Response.AddHeader("Content-Length", file.Length.ToString()); //指定文件大小
jc.Context.Response.ContentType = "application/application/octet-stream"; //指定输出方式
jc.Context.Response.WriteFile(file.FullName); //写出文件
jc.Context.Response.Flush(); //输出缓冲区(刷新Response对象)
jc.Context.Response.Clear(); //清空Response对象
jc.Context.Response.End(); //结束Response对象
}
导出文件不能用post或者get来导出,必须是url请求
asp.net 下载任意格式文件 上传文件后台代码的更多相关文章
- BootStrap fileinput.js文件上传组件实例代码
1.首先我们下载好fileinput插件引入插件 ? 1 2 3 <span style="font-size:14px;"><link type="t ...
- Apache Flink任意Jar包上传导致远程代码执行漏洞复现
0x00 简介 Apache Flink是由Apache软件基金会开发的开源流处理框架,其核心是用Java和Scala编写的分布式流数据流引擎.Flink以数据并行和流水线方式执行任意流数据程序,Fl ...
- plupload批量上传分片(后台代码)
plupload批量上传分片功能, 对于文件比较大的情况下,plupload支持分片上传,后台代码如下: /** * * 方法:upLoadSpecialProgramPictrue * 方法说明:本 ...
- 【要什么自行车】ASP.NET MVC4笔记02:上传文件 uploadify 组件使用
参考:http://www.cnblogs.com/luotaoyeah/p/3321070.html 1.下载 uploadify 组件,copy至 Content文件夹 <link href ...
- 【python】用python脚本Paramiko实现远程执行命令、下载、推送/上传文件功能
Paramiko: paramiko模块,基于SSH用于连接远程服务器并执行相关操作. SSHClient: 用于连接远程服务器并执行基本命令 SFTPClient: 用于连接远程服务器并执行上传下载 ...
- 通达OA任意文件上传+文件包含GetShell/包含日志文件Getshell
0x01 简介 通达OA采用基于WEB的企业计算,主HTTP服务器采用了世界上最先进的Apache服务器,性能稳定可靠.数据存取集中控制,避免了数据泄漏的可能.提供数据备份工具,保护系统数据安全.多级 ...
- ASP.NET中扩展FileUpload的上传文件的容量
ASP.NET中扩展FileUpload只能上传小的文件,大小在4MB以内的.如果是上传大一点的图片类的可以在web.config里面扩展一下大小,代码如下 <system.web> &l ...
- ASP.NET - 多文件上传,纯代码,不使用插件
解决方案: 前段代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Mu ...
- ASP.NET MVC中使用表单上传文件时的注意事项
最近了好久没写ASP.NET 使用HTML的FORM来上传文件了,结果写了个文件上传发现ASP.NET MVC的Controller中老是读取不到上传的文件. MVC的View(Index.cshtm ...
- java http下载文件/上传文件保存
private boolean downloadFile(String httpUrl, String savePath) { int byteread = 0; try { URL url = ne ...
随机推荐
- NET Core 以及与 .NET Framework
简析.NET Core 以及与 .NET Framework的关系 简析.NET Core 以及与 .NET Framework的关系 一 .NET 的 Framework 们 二 .NET Core ...
- 浙江大学PAT上机题解析之3-04. 一元多项式的乘法与加法运算
设计函数分别求两个一元多项式的乘积与和. 输入格式说明: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分 ...
- Android---60---Notification 通知栏的简单使用
Notification是显示在手机状态栏的通知 通过Notification.Builder类创建Notification对象. Notification.Builder经常用法: setDefau ...
- PHP - 概述
第1章 PHP概述 学习要点: 1.PHP基础知识 2.PHP的环境配置 3.安装三款主流程序 4.PHP开发工具的选择 5.一个简单的示例 一.PHP基础知识 PHP PHP是一种目前最流行的服务端 ...
- Session 转台服务器的使用方法
Session的缺陷:为了保持自身的稳定,IIS在访问量大的时候,可能会不自觉的重启,这时候Session就会丢失用户就会被迫下线 解决方案1:将Session放到一个专门的转台服务器上 方案2:将S ...
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3457&rd=5869 解答分析:http://comm ...
- 遍历关联数组 index by varchar2
--字符串序列要这样 declare type t is table of number(3) index by varchar2(3); hash_t t; l_row ...
- 如何删除JAVA集合中的元素
经常我们要删除集合中的某些元素.有些可能会这么写. public void operate(List list){ for (Iterator it = list.iterator(); it.has ...
- 关于Get和Post
get和post 简介: Get和post是表单提交数据的两种基本方式,get请求数据通过域名后缀url传送,用户可见,不安全,post请求数据通过在请求报文正文里传输,相对比较安全. get是通过u ...
- Silverlight技术调查(3)——国际化
原文 Silverlight技术调查(3)——国际化 网上有很多关于Silverlight国际化的说明,包括MSDN的示例,都没有强调一点,下面以红色标示,基础国际化知识请先参考MSDN相关章节,关键 ...