mvc上传,下载,浏览文件功能(用uploadify插件)

 public class UpLoadFileController : Controller
{
//
// GET: /UpLoadFile/ public ActionResult Index()
{
return View();
} /// <summary>
/// 上传文件【上传文件到UploadFiles文件夹】
/// </summary>
/// <param name="fileData"></param>
/// <param name="Id"></param>
/// <returns></returns>
[AcceptVerbs(HttpVerbs.Post)]
public ContentResult UpLoadFile(HttpPostedFileBase fileData, String Id)
{
ControllerContext.HttpContext.Request.ContentEncoding = Encoding.UTF8;
ControllerContext.HttpContext.Response.ContentEncoding = Encoding.UTF8;
ControllerContext.HttpContext.Response.Charset = "UTP-8"; string filePath = Server.MapPath(@"/UploadFiles");
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
if (fileData != null)
{
string fileSaveName = Path.Combine(filePath, fileData.FileName);
fileData.SaveAs(fileSaveName);
return Content("成功");
}
return Content("失败");
} /// <summary>
/// 获取文件【下载】
/// </summary>
/// <param name="id">文件名</param>
/// <returns></returns>
public FilePathResult GetFile(string id)
{
if (string.IsNullOrEmpty(id))
{
return null;
}
id = id.Replace("__", ".");
string path = "UploadFiles";
string filePath = Server.MapPath(@"/" + path);
string fileSaveName = Path.Combine(filePath, id);
return File(fileSaveName, "image/*", id);
} }

Index.cshtml

@{
ViewBag.Title = "上传文件";
}
<script src="~/Content/uploadify/jquery.uploadify.js?ver=@DateTime.Now.Ticks"></script>
<link href="~/Content/uploadify/uploadify.css" rel="stylesheet" /> @*上传文件*@
<div>
<input class="easyui-validatebox" type="hidden" id="Attachment_GUID" name="Attachment_GUID" />
<input id="file_upload" name="file_upload" type="file" >
@* <a href="javascript:void(0)" class="easyui-linkbutton" id="btnUpload" data-options="plain:true,iconCls:'icon-save'"
onclick="javascript: $('#file_upload').uploadify('upload', '*')">上传</a>
<a href="javascript:void(0)" class="easyui-linkbutton" id="btnCancelUpload" data-options="plain:true,iconCls:'icon-cancel'"
onclick="javascript: $('#file_upload').uploadify('cancel', '*')">取消</a>
<div id="fileQueue" class="fileQueue"></div>*@
<a href="javascript:void(0)" class="easyui-linkbutton" id="btnBrowse" >浏览</a>
<div id="div_files">
<a id="downFile" href="#">下载文件</a>
<img id="uploadImg" src=""/>
</div>
<br /> </div> @*弹出层*@
@Html.Partial("Contact") <script> $(function () {
$("#btnBrowse").click(function () {
PopLayout.Pop($("#uploadImg").attr("src"));
}); $("#file_upload").uploadify({
height: 30,
width: 120,
buttonText: "选择",
multi: false,
swf: '/Content/uploadify/uploadify.swf',
uploader: '@Url.Action("UpLoadFile","UpLoadFile")',
onFallback: function () {
alert("不支持flash");
},
onUploadError: function (file, errorCode, errorMsg, errorString) {
alert("文件" + file.name + "上传失败: " + errorString);
},
onUploadSuccess: function (file, data, response) {
if (true) {
//alert(file.name);
var id = file.name.replace(".", "__");;
$("#downFile").attr("href", "/UpLoadFile/GetFile/" + id);
$("#uploadImg").attr("src", "/UploadFiles/" + file.name);
}
//alert("文件" + file.name + "上传成功:" + data);
}
});
}); </script>

@Html.Partial("Contact")  嵌入Contact.cshtml文件

请看上篇文章,弹出层:http://www.cnblogs.com/liujinwu-11/p/4334568.html

上一篇:android直接读取数据库文件


下一篇:在腾讯云上创建您的SQL Cluster(4)