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 ...
随机推荐
- JQuery遍历json数组的3种方法
这篇文章主要介绍了JQuery遍历json数组的3种方法,本文分别给出了使用each.for遍历json的方法,其中for又分成两种形式,需要的朋友可以参考下 一.使用each遍历 $(functio ...
- cocos2d-x游戏开发系列教程-中国象棋03-主界面
前情回顾 上个博客说道我们象棋程序进入了欢迎界面,在欢迎界面下等待一秒进入主界面 进入主界面的关键代码如下: CCScene* pScene = CCMainMenu::scene(); 创建sce ...
- 一张图说清Asp.NET MVC中的 RenderPage、RenderBody、RenderSection
- Oracle数据库索引使用及索引失效总结
容易引起oracle索引失效的原因很多: 1.在索引列上使用函数.如SUBSTR,DECODE,INSTR等,对索引列进行运算.需要建立函数索引就可以解决了. 2.新建的表还没来得及生成统计信息,分析 ...
- IOS之【属性列表】
@implementation JamesWongViewController - (void)viewDidLoad { [superviewDidLoad]; [selfwritePerson]; ...
- ctfmon.exe开机无法自己主动启动
打开命令提示符(開始菜单--执行--输入:cmd),输入下面命令(复制粘贴就可以): reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Ru ...
- 【模式识别】Boosting
Boosting简单介绍 分类中通常使用将多个弱分类器组合成强分类器进行分类的方法,统称为集成分类方法(Ensemble Method).比較简单的如在Boosting之前出现Bagging的方法,首 ...
- android ble蓝牙开发略解
Android 蓝牙4.0开发 1. 权限和相关属性 “android:required="true"表示apk只有在具有bluetooth_le属性的系统里运行,这个4.3之前 ...
- PHP学习之-数据库操作
PHP学习之-数据库操作 1.PHP支持那些数据库 PHP通过安装相应的扩展来实现数据库操作,现代应用程序的设计离不开数据库的应用,当前主流的数据库有MsSQL,MySQL,Sybase,Db2,Or ...
- 学习老外用webstorm开发nodejs的技巧--代码提示DefinitelyTyped
最近入了nodejs的坑,作为老码农,js对我来说还是很容易的.webstorm虽说用得不多,但是pycharms我是老手了,idea的东西一脉相承,想想也就那样了. 但是自从看了某个视频后,觉得毕竟 ...