思路:将文件转化为流,输出到页面上的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 下载任意格式文件 上传文件后台代码的更多相关文章

  1. BootStrap fileinput.js文件上传组件实例代码

    1.首先我们下载好fileinput插件引入插件 ? 1 2 3 <span style="font-size:14px;"><link type="t ...

  2. Apache Flink任意Jar包上传导致远程代码执行漏洞复现

    0x00 简介 Apache Flink是由Apache软件基金会开发的开源流处理框架,其核心是用Java和Scala编写的分布式流数据流引擎.Flink以数据并行和流水线方式执行任意流数据程序,Fl ...

  3. plupload批量上传分片(后台代码)

    plupload批量上传分片功能, 对于文件比较大的情况下,plupload支持分片上传,后台代码如下: /** * * 方法:upLoadSpecialProgramPictrue * 方法说明:本 ...

  4. 【要什么自行车】ASP.NET MVC4笔记02:上传文件 uploadify 组件使用

    参考:http://www.cnblogs.com/luotaoyeah/p/3321070.html 1.下载 uploadify 组件,copy至 Content文件夹 <link href ...

  5. 【python】用python脚本Paramiko实现远程执行命令、下载、推送/上传文件功能

    Paramiko: paramiko模块,基于SSH用于连接远程服务器并执行相关操作. SSHClient: 用于连接远程服务器并执行基本命令 SFTPClient: 用于连接远程服务器并执行上传下载 ...

  6. 通达OA任意文件上传+文件包含GetShell/包含日志文件Getshell

    0x01 简介 通达OA采用基于WEB的企业计算,主HTTP服务器采用了世界上最先进的Apache服务器,性能稳定可靠.数据存取集中控制,避免了数据泄漏的可能.提供数据备份工具,保护系统数据安全.多级 ...

  7. ASP.NET中扩展FileUpload的上传文件的容量

    ASP.NET中扩展FileUpload只能上传小的文件,大小在4MB以内的.如果是上传大一点的图片类的可以在web.config里面扩展一下大小,代码如下 <system.web> &l ...

  8. ASP.NET - 多文件上传,纯代码,不使用插件

    解决方案: 前段代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Mu ...

  9. ASP.NET MVC中使用表单上传文件时的注意事项

    最近了好久没写ASP.NET 使用HTML的FORM来上传文件了,结果写了个文件上传发现ASP.NET MVC的Controller中老是读取不到上传的文件. MVC的View(Index.cshtm ...

  10. java http下载文件/上传文件保存

    private boolean downloadFile(String httpUrl, String savePath) { int byteread = 0; try { URL url = ne ...

随机推荐

  1. BZOJ 3211: 花神游历各国( 线段树 )

    线段树...区间开方...明显是要处理到叶节点的 之前在CF做过道区间取模...差不多, 只有开方, 那么每个数开方次数也是有限的(0,1时就会停止), 最大的数10^9开方10+次也就不会动了.那么 ...

  2. JavaScript下全选反选的Demo程序里实现checkmeonly函数 DOM

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. TimeUnit

    转http://blog.csdn.net/hudashi/article/details/6936604 public enum TimeUnitextends Enum<TimeUnit&g ...

  4. WCF技术剖析之二十七: 如何将一个服务发布成WSDL[基于WS-MEX的实现](提供模拟程序)

    原文:WCF技术剖析之二十七: 如何将一个服务发布成WSDL[基于WS-MEX的实现](提供模拟程序) 通过<如何将一个服务发布成WSDL[编程篇]>的介绍我们知道了如何可以通过编程或者配 ...

  5. 创建用于编译和运行Java程序的批处理文件

    创建用于编译和运行Java程序的批处理文件 每次编译或运行Java程序时,都要在DOS命令行中输入很长的javac或java命令,悟空觉得太麻烦,就编写了一个适用于Windows操作系统的批处理文件b ...

  6. maven setting配置

    <?xml version="1.0" encoding="UTF-8"?> <!--Licensed to the Apache Softw ...

  7. jQuery.localStorage() - jQuery SDK API

    jQuery.localStorage() - jQuery SDK API jQuery.localStorage() From jQuery SDK API   Jump to: navigati ...

  8. Hauntbox:用于控制你的自己主动化、电子创意家居的开源硬件盒子

    Hauntbox 是一个开源硬件控制器,能够满足用随意传感器和控制器建立复杂的.自己主动化的萦绕在心头的电子项目. 它不须要焊接或者预先学什么知识.是全然可控制.并与Arduino插板兼容. 无需编程 ...

  9. 关于android多点触控

    最近项目需要一个多点触控缩放的功能.然后上网查了下资料 总结一下: 首先android sdk版本很重要,比如你在AndroidManifest.xml中指定android:minSdkVersion ...

  10. 新发现的Cyberduck(映射网盘)和zsuncloud(硬件产品很新潮),群辉nas的确好用(购买链接)

    https://cyberduck.io/?l=en http://www.zsuncloud.com/ 群辉nas的确好用啊在哪里可以买到?官网 淘宝也可以自己做黑群晖 先用xpenoboot is ...