MVC 上传 下载
【上传】带进度条
view
注:添加easyui的js文件
<script type="text/javascript">
function fileSelected() {
var file = document.getElementById('fileToUpload').files[0];
var fileName = file.name;
var file_typename = fileName.substring(fileName.lastIndexOf('.'), fileName.length);
if (file_typename == '.doc' || file_typename == '.docx') {//这里限定上传文件文件类型
if (file) {
$("#uploadFile").show();
var fileSize = 0;
if (file.size > 1024 * 1024)
fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
else
fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
document.getElementById('fileName').innerHTML = '文件名: ' + file.name;
document.getElementById('fileSize').innerHTML = '大小: ' + fileSize;
document.getElementById('fileType').innerHTML = '类型: ' + file.type;
}
}
else {
$("#uploadFile").hide();
document.getElementById('fileName').innerHTML = "<span style='color:Red'>错误提示:上传文件应该是.doc后缀而不应该是" + file_typename + ",请重新选择文件</span>"
document.getElementById('fileSize').innerHTML = "";
document.getElementById('fileType').innerHTML = "";
}
}
function uploadFile() {
var fd = new FormData();
fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]);
var XMLHttp = null;
if (window.XMLHttpRequest) { //测试 window.XMLHttpRequest 对象是否可用
var xhr = new XMLHttpRequest(); //创建一个新对象
xhr.upload.addEventListener("progress", uploadProgress, false);
//监听事件
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "/Manage/Upload");
xhr.send(fd);
} else if (window.ActiveXObject) {
var xhr = new ActiveXObject("Microsoft.XMLHTTP");
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "/Manage/Upload");
xhr.send(fd);
}
}
//上传进度
function uploadProgress(evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total);
$('#progressNumber').progressbar('setValue', percentComplete);
}
else {
document.getElementById('progressNumber').innerHTML = '无法计算';
}
}
//上传成功响应
function uploadComplete(evt) {
var message = evt.target.responseText;
alert(message);
}
//上传失败
function uploadFailed(evt) {
alert("上传出错");
}
//取消上传
function uploadCanceled(evt) {
alert("上传已由用户或浏览器取消删除连接");
}
</script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div>
<div class="row" style="padding: 10px">
<div>
论文题目:
<input class="easyui-validatebox easyui-textbox" data-options="required:true" name="Re_Title"
id="Re_Title" style="width: 250px" />
</div>
<label for="file">
论文内容:
</label>
<input type="file" name="fileToUpload" id="fileToUpload" multiple="multiple" onchange="fileSelected();" />
<a id="uploadFile" style="display: none" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'"
onclick="uploadFile()">上传</a>
</div>
<div id="fileName" style="padding: 10px">
</div>
<div id="fileSize" style="padding: 10px">
</div>
<div id="fileType" style="padding: 10px">
</div>
<div id="progressNumber" class="easyui-progressbar" style="width: 400px; float: left;
margin-left: 10px;">
</div>
<div style="padding: 95px">
<input id="submit" type="submit" value="保存" class="bottom" />
<input id="reset" type="reset" value="取消" class="bottom" />
</div>
</div>
}
控制器 Controller.cs
/// <summary>
/// 上传文件
/// </summary>
/// <param name="fileToUpload"></param>
/// <returns></returns>
[HttpPost]
[ValidateInput(false)]
public string Upload(HttpPostedFileBase[] fileToUpload)
{
try
{
string FileUrl = string.Empty;
foreach (HttpPostedFileBase file in fileToUpload)
{
string path = System.IO.Path.Combine(Server.MapPath("~/FileUpLoad/Reviews"), System.IO.Path.GetFileName(file.FileName));
file.SaveAs(path);
FileUrl = path;
}
Session["URL"] = FileUrl;
return "上传成功";
}
catch
{
return "上传失败";
}
}
【下载】
view
<a href="/Manage/DownFile?filePath=@Model.Re_Content&fileName=@Model.Re_Title">@Model.Re_Title</a>
controller.cs
#region ReviewDownFile
//本地路径转换成URL相对路径
private string urlconvertor(string imagesurl1)
{
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
string imagesurl2 = imagesurl1.Replace(tmpRootDir, ""); //转换成相对路径
imagesurl2 = imagesurl2.Replace(@"\", @"/");
return imagesurl2;
}
//下载方法
[HttpGet]
public FileStreamResult DownFile(string filePath, string fileName)
{
var url = filePath;
var newurl = urlconvertor(url);
string absoluFilePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["AttachmentPath"] + newurl);
absoluFilePath = absoluFilePath.Replace("\\Manage", "");
return File(new FileStream(absoluFilePath, FileMode.Open), "application/octet-stream", Server.UrlEncode(fileName));
}
#endregion
MVC 上传 下载的更多相关文章
- Spring框架学习(8)spring mvc上传下载
内容源自:spring mvc上传下载 如下示例: 页面: web.xml: <?xml version="1.0" encoding="UTF-8"?& ...
- MVC 上传下载
在Asp.net的WEBform中,上传文件与下载文件处理是很简单的事情,如果转为ASP.NET MVC呢?那就没有那么容易了,难少少,也不是很难,一起来看下本文吧.本文主要讲如何在Asp.net M ...
- asp.net mvc 上传下载文件的几种方式
view: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...
- spring mvc上传下载文件
前端jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- 基于Spring Mvc实现的Excel文件上传下载
最近工作遇到一个需求,需要下载excel模板,编辑后上传解析存储到数据库.因此为了更好的理解公司框架,我就自己先用spring mvc实现了一个样例. 基础框架 之前曾经介绍过一个最简单的spring ...
- Spring MVC 使用介绍(十四)文件上传下载
一.概述 文件上传时,http请求头Content-Type须为multipart/form-data,有两种实现方式: 1.基于FormData对象,该方式简单灵活 2.基于<form> ...
- mvc中文件上传下载
//控制器 public ActionResult FileUpLoad(HttpPostedFileBase f1) { string path = Server.MapPath("~/P ...
- C# --MVC实现简单上传下载
首先创建一个默认的控制器Defaultcontroller 然后生成视图View 在视图里面 创建文件选择器 创建上传.下载按钮 代码如下 <body> <div> <f ...
- springmvc 上传下载
springmvc文件上传下载在网上搜索的代码 参考整理了一份需要使用的jar.commons-fileupload.jar与commons-io-1.4.jar 二个文件 1.表单属性为: enct ...
随机推荐
- CoreAnimation 之CAReplicatorLayer
CAReplicatorLayer: 主要作用有以下两个: CAReplicatorLayer的目的是为了高效生成许多相似的图层,它会绘制一个或多个图层的子图层 并在每个复制体上应用不同的变换 使用C ...
- 使用原生ajax处理json组成的数组
和前一篇文章一样,直接上代码了,只是做个记录. 数据的提供页面,tigong.php <?php header("content-type:text/html;charset=utf- ...
- Rest(表述性状态转移)
本文的主要内容有: 1.了解Rest 2.了解RESTful WebService 3.使用SpringMvc实现RESTful ------------------------------我是华丽的 ...
- C语言笔记一
学习C语言已经有一段时间,然而发现越学不知道的东西越多,这是在印象笔记中记得一些东西,现在再回顾一遍顺便补充一些新东西. 一,基础知识 运算符号 优先级 单目>算术>关系 从高到低 ...
- python备忘
1.引用已经编写好的.py文件(Windows系统) >>>import sys >>>sys.path.append("C:/python") ...
- 字符串的replace()方法隐藏着什么不可告人秘密?
最近在做JS算法项目时发现一个令我匪夷所思的问题, 这里想记录一下问题. 首先介绍一下字符串replace()方法的基本用法. replace() 方法使用一个替换值(replacement)替换掉一 ...
- c# 针对SAP服务通讯
对于sap完全没有概念. 不知道是什么,也不想了解过多.还是像针对一个技能好好的研究一下. 年前的一个项目遇到c#调用SAP来实现一些业务逻辑对于我这个门外汉确实有点摸不着头闹.捋顺一下思路. . 结 ...
- RocketMQ与kafka对比(18项差异)-转自阿里中间件
淘宝内部的交易系统使用了淘宝自主研发的Notify消息中间件,使用Mysql作为消息存储媒介,可完全水平扩容,为了进一步降低成本,我们认为存储部分可以进一步优化,2011年初,Linkin开源了Kaf ...
- 【Exception—WebForm】当应用程序不是以 UserInteractive 模式运行时显示模式对话框或窗体是无效操作。请指定 ServiceNotification 或 DefaultDesktopOnly 样式,以显示服务应用程序发出的通知。
最近做的项目现在发布到服务器上开始测试了,本地好好的程序,到服务器上却报异常了: 当应用程序不是以 UserInteractive 模式运行时显示模式对话框或窗体是无效操作.请指定 ServiceNo ...
- Unity4.0的使用
最近公司用到了Unity,自己就研究了一下. 新建一个ASP.NET MVC基本项目,在NuGet上引入Unity4.0.1最新版. 因为我使用的项目为ASP.NET MVC,所以又添加一个Unity ...