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 ...
随机推荐
- mac忘记登陆密码解决
重新启动苹果电脑,开机时按住“command”键+“S”键,(普通键盘按住win+s)会进入单用户模式, 出现像DOS一样的提示符 #root>依次输入如下三个命令:(注意空格 注意大小写) f ...
- jsp各部分编码的含义
服务器JSP编码 pageEncoding 是jsp文件本身的编码, 第一阶段是jsp编译成.java,它会根据pageEncoding的设定读取jsp,(jsp文件的编码,pageEncoding是 ...
- Cubieboard4卡片式电脑
Cubieboard4 also named CC-A80, is a open source mini PC or single board computer which has ultra-pow ...
- K&R练习题6-1统计关键词出现的次数
这道练习题训练了: 1.结构体数组 2.二分查找 3.指针操作 ---- 都不难.但非常基础,我认为非常好,做完了记到博客上来,题目见k&R,实现例如以下: /* * Practice of ...
- ARMv8 Linux内核源代码分析:__flush_dcache_all()
1.1 /* * __flush_dcache_all() * Flush the wholeD-cache. * Corrupted registers: x0-x7, x9-x11 */ EN ...
- Java基础04 封装与接口
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 总结之前的内容,对象(object)指代某一事物,类(class)指代象的类型.对 ...
- SQL Server 2008中文企业版下载地址和序列号[转]
SQLSERVER2008下载链接http://sqlserver.dlservice.microsoft.com/dl/download/B/8/0/B808AF59-7619-4A71-A447- ...
- 基于visual Studio2013解决C语言竞赛题之1019填数
题目 解决代码及点评 /* 19. 找3个数字,填在下面式子中,使等式成立. _6325 = 6325_ × ____ (等号左边是五位) 1,若答案有多个,则打印一组即 ...
- Session for Tornado(Redis) - 代码分享
Session for Tornado(Redis) - 代码分享 Session for Tornado(Redis) session id的生成借用了web.py. 使用了 redis 的 h ...
- skip32
在某些应用场景下,需要对数字进行加密,skip32无疑是一种很实用的算法,网上有python的源码(https://bitbucket.org/anuraguniyal/skip32.py/src/4 ...