一、前端cshtml代码

 <tr>
<td width="130" align="right">添加附件:</td>
@using (Html.BeginForm("FileUp", "Detail", FormMethod.Post, new { enctype = "multipart/form-data", id = "formFileUpload" }))
{ <td>
<input name="file" type="file" value="浏览..." id="uploadFile" style="display: none;" />
<input type="text" class="fj_nr" id="filePath" />
</td>
<td>
<input type="button" value="浏览..." class="fh_btn" id="btnSee">
<input type="submit" value="上传" class="fh_btn" id="btnUploadFile" />
</td>
}
</tr>
<tr id="attachMents">
</tr>

二、JS代码

(function () {
var replyJs = replyJs || {};
replyJs.unitls = (function () {
var controller = '/RenosData.Fax.Web/Detail';
var homeController = '/RenosData.Fax.Web/Home';
//传真回复
var replyFax = function () {
//浏览附件
$("#btnSee").bind("click", function (e) {
$("#uploadFile").click();
});
//上传附件
$("#uploadFile").bind("change", function (e) {
$("#filePath").val($("#uploadFile").val());
});
$("#btnUploadFile").click(function () {
addAttachment();
return false;
}); //删除传真附件
$("#btnDelAttach").live("click",function () {
$.ajax({
url: controller + '/DelAttachment',
type: 'post',
//dataType: 'json',
data: "{file:'" + $(this).attr("filename") + "'}",
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data.status == "success") {
$("#attachMents").empty();
} else {
alert(data.message);
}
},
error: function (err) {
alert(err.toString());
}
});
});
//发送传真
$("#btnSendFax").click(function () {
addFaxToDb("send");
});
};
//上传附件
var addAttachment = function () {
if (!$("#filePath").val()) {
alert("请选择需要上传的文件!");
return;
}
//function showRequest(formData, jqForm, options) {
// //alert('发送前');
// return true;
//}
//function showResponse(responseText, statusText) {
// //alert('发送后');
//}
//var options = {
// //target: '#outputdiv',
// beforeSubmit: showRequest,
// success: showResponse
//};
//$(this).ajaxSubmit(options);
$("#formFileUpload").ajaxSubmit({
dataType: 'json',
beforeSend: function (xhr) { },
success: function (data) {
if (data) {
if (data.message == "success") {
$("#filePath").val("");
$("#attachMents").empty().append("<td width='130' align='right'>已上传附件:</td><td><label id='lblFileName'>" + data.fileOldName + "</label></td><td><input type='button' value='删除' class='fh_btn' id='btnDelAttach' filename='" + data.fileName + "' filesize='" + data.fileSize + "'></td>");
} else {
alert(data.message);
}
}
},
complete: function () { }
});
return;
};
//传真状态:发送or保存 var back = function () {
window.history.go(-1);
}; return {
replyFax: replyFax,
};
}()); $(function () {
replyJs.unitls.replyFax();
});
})(jQuery);

三、Controller代码

 /// <summary>
/// 添加附件
/// </summary>
/// <returns></returns>
[HttpPost]
public ActionResult FileUp()
{
HttpPostedFileBase uploadFile = Request.Files[];
var fax = new FaxModel();
if (uploadFile == null || uploadFile.ContentLength == )
{
fax = new FaxModel() { Message = "请选择上传附件!", Attachment = null };
return Json(new { message = fax.Message });
}
//if (uploadFile.ContentLength > 20971520)
//{
// fax = new FaxModel() { Message = "请上传20MB以内的附件!", Attachment = null };
// return View("NewFax", fax);
//}
try
{
var newFileName = Guid.NewGuid() + "_" + uploadFile.FileName; ;
string attachFilePath = WebConfig.Attachment;
if (!Directory.Exists(attachFilePath))
Directory.CreateDirectory(attachFilePath);
string filePath = Path.Combine(attachFilePath, newFileName);
uploadFile.SaveAs(filePath);
var attachment = new FileAttachmentModel()
{
FileName = newFileName,
FileLength = (uploadFile.ContentLength * 1.0 / ).ToString("0.00"),
FilePath = filePath,
FileOldName = uploadFile.FileName,
FileSize = uploadFile.ContentLength
};
fax = new FaxModel() { Attachment = attachment };
ViewBag.FaxMsg = uploadFile.FileName;
return Json(new { message = "success", fileOldName = attachment.FileOldName, fileSize = attachment.FileSize, fileName = attachment.FileName });
}
catch (Exception)
{
return Json(string.Empty);
}
}

附件删除

 /// <summary>
/// 附件删除
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
[HttpPost]
public ActionResult DelAttachment(string file)
{
try
{
string attachFilePath = WebConfig.Attachment;
string filePath = Path.Combine(attachFilePath, file);
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
return Json(new { status = "success" });
}
return Json(new { status = "false", message = "附件删除失败!" });
}
catch (Exception ex)
{
return Json(new { status = "false", message = "附件删除失败!" });
}
}

ajaxForm.js

MVC + ajaxform 文件上传的更多相关文章

  1. Spring MVC实现文件上传

    基础准备: Spring MVC为文件上传提供了直接支持,这种支持来自于MultipartResolver.Spring使用Jakarta Commons FileUpload技术实现了一个Multi ...

  2. MVC之文件上传1

    MVC之文件上传 前言 这一节我们来讲讲在MVC中如何进行文件的上传,我们逐步深入,一起来看看. Upload File(一) 我们在默认创建的项目中的Home控制器下添加如下: public Act ...

  3. Asp.net mvc 大文件上传 断点续传

    Asp.net mvc 大文件上传 断点续传 进度条   概述 项目中需要一个上传200M-500M的文件大小的功能,需要断点续传.上传性能稳定.突破asp.net上传限制.一开始看到51CTO上的这 ...

  4. Spring MVC的文件上传

    1.文件上传 文件上传是项目开发中常用的功能.为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data.只有在这种情况下,浏览器才会把用户 ...

  5. Spring MVC的文件上传和下载

    简介: Spring MVC为文件上传提供了直接的支持,这种支持使用即插即用的MultipartResolver实现的.Spring MVC 使用Apache Commons FileUpload技术 ...

  6. 整合MVC实现文件上传

    1.整合MVC实现文件上传整合MVC实现文件上传在实际的开发中在实现文件上传的同时肯定还有其他信息需要保存到数据库,文件上传完毕之后需要将提交的基本信息插入数据库,那么我们来实现这个操作.整个MVC实 ...

  7. 【Spring学习笔记-MVC-13】Spring MVC之文件上传

    作者:ssslinppp       1. 摘要 Spring MVC为文件上传提供了最直接的支持,这种支持是通过即插即用的MultipartResolve实现的.Spring使用Jakarta Co ...

  8. MVC图片上传、浏览、删除 ASP.NET MVC之文件上传【一】(八) ASP.NET MVC 图片上传到服务器

    MVC图片上传.浏览.删除   1.存储配置信息 在web.config中,添加配置信息节点 <appSettings> <add key="UploadPath" ...

  9. spring mvc ajaxfileupload文件上传返回json下载问题

    问题:使用spring mvc ajaxfileupload 文件上传在ie8下会提示json下载问题 解决方案如下: 服务器代码: @RequestMapping(value = "/ad ...

随机推荐

  1. The difference between the request time and the current time is too large.阿里云oss上传图片报错

    The difference between the request time and the current time is too large. 阿里云oss上传图片的时候报错如上, 解决办法,把 ...

  2. java中字符串太长,怎么自动换到下一行

    直接在中间代码出按回车

  3. Unity打包IOS和Android以及之间的交互

    1.导出的Xcode工程 主要讲解Unity导出的Xcode工程的目录结构 2.导出的Android-Eclipse工程 主要讲解Unity导出的Android-Eclipse工程的目录结构 3.导出 ...

  4. MathType让矩阵中的小数以小数点对齐的教程

    用MathType编辑公式时,有很多模板都可以应用,并且这些模板并不是只有一种用途,比如矩阵.矩阵模板可以用来编辑矩阵也可是用来编辑一些需要排列组合的数据等等.在用MathType编辑矩阵时,如果里面 ...

  5. python装饰器、继承、元类、mixin,四种給类动态添加类属性和方法的方式(一)

    介绍装饰器.继承.元类.mixin,四种給类动态添加类属性和方法的方式 有时候需要給类添加额外的东西,有些东西很频繁,每个类都需要,如果不想反复的复制粘贴到每个类,可以动态添加. # coding=u ...

  6. vs2012修复问题

    多装了一个.net framework4.5.1结果vs不能拥,借用了下面这个工具将vs2012从注册表中删除了 就能重装了 http://www.auslogics.com/en/software/ ...

  7. flexbox常用布局左右固定,中间自适应

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  8. 【PHP】phpstudy vhosts.conf 配置

    #Listen 876 <VirtualHost *:876> ServerName localhost DocumentRoot "D:\phpStudy\PHPTutoria ...

  9. 【VTK】VTK 之一 环境准备

    VTK总结 http://blog.sina.com.cn/s/articlelist_2216172153_3_1.html VTK教程http://blog.csdn.net/www_doling ...

  10. Unity Shader 获取模型空间坐标

    CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma ...