mvc框架上传文件-2

来源:互联网 发布:数据录入外包 编辑:程序博客网 时间:2024/06/15 16:53
 上篇介绍的是如何传文件,本篇介绍如何显示文件并打开或者下载文件。 HTML:
  <div id="lookFiles" >                    <a id="downFile2" target="_blank">                        <div><span id="filename2" style="width:100%;"></span></div>                        <div class="file" style="border:0"><img style="width: 120px; height:120px;"  id="imghead2" /></div>                    </a>                </div>

jquery:

 ehr.ajax(ehr.servicesUrl + 'Receipt/GetPageAnnex', { "reqId": reqId }, function (json) {            if (json.Value.NewDataSet == null) {                            return;            }            var alldata = json.Value.NewDataSet.Table;            var dom = '';            if (!(alldata instanceof Array)) {                //将返回的对象转换为数组                alldata = transform(alldata);            }            var fileName = alldata[0].fileName;            $("#filename2").text(fileName);            var point = fileName.lastIndexOf(".");            var type = fileName.substr(point);            showImage(type);            ehr.ajax(ehr.servicesUrl + 'Affair/DownFile', { "downFileName": fileName, "downFileContent": alldata[0].annexContent }, function (json) {                $("#downFile2").attr("href", json.Value.Table.Path);            });        });
 //判断后缀名,展示不同的图片    function showImage(type) {        var img = "";        if (type == '.7Z' || type == '.7z') { img = "../Content1/img/7Z.png"; }        if (type == '.bmp' || type == '.BMP') { img = "../Content1/img/BMP.png"; }        if (type == '.DOC' || type == '.doc') { img = "../Content1/img/DOC.png"; }        if (type == '.DOCX' || type == '.docx') { img = "../Content1/img/DOCX.png"; }        if (type == '.JPEG' || type == '.jpeg') { img = "../Content1/img/JPEG.png"; }        if (type == '.jpg' || type == '.JPG') { img = "../Content1/img/JPG.png"; }        if (type == '.PDF' || type == '.pdf') { img = "../Content1/img/PDF.png"; }        if (type == '.png' || type == '.PNG') { img = "../Content1/img/PNG.png"; }        if (type == '.RAR' || type == '.rar') { img = "../Content1/img/RAR.png"; }        if (type == '.txt' || type == '.TXT') { img = "../Content1/img/TXT.png"; }        if (type == '.xls' || type == '.XLS') { img = "../Content1/img/XLS.png"; }        if (type == '.XLSX' || type == '.xlsx') { img = "../Content1/img/XLSX.png"; }        if (type == '.zip' || type == '.ZIP') { img = "../Content1/img/ZIP.png"; }        $("#imghead2").attr("src", img);            }

后台代码:

  public ActionResult GetPageAnnex(string reqID)        {            JsonRult result = new JsonRult();            string empid = Session["uID"] == null ? "" : Session["uID"].ToString();            if (empid == "")            {                result.Message = "请先登录";                result.Success = false;                result.Value = "";                var rultResult = Json(result, JsonRequestBehavior.AllowGet);                return rultResult;            }            else            {                EHR.EhrWebService.Android_ServiceSoapClient cli = new EhrWebService.Android_ServiceSoapClient();                string key = Session["key"] == null ? "" : Session["key"].ToString();                string sMsg = null;                var item = cli.GetPageAnnex(reqID, ref sMsg);                result.Message = sMsg;                result.Value = item;                result.Success = (sMsg == "1" ? true : false);                if (result.Success)                {                    XmlDocument doc = new XmlDocument();                    doc.LoadXml(item);                    result.Value = Newtonsoft.Json.JsonConvert.SerializeObject(doc);                }                var rultResult = Json(result, JsonRequestBehavior.AllowGet);                return rultResult;            }        }
  public ActionResult DownFile(string downFileName, string downFileContent)        {            try            {                string pathFolder = Server.MapPath("~/Content1/downLoad");                if (!Directory.Exists(pathFolder))                {                    Directory.CreateDirectory(pathFolder);                }                string save_path = Server.MapPath("~/Content1/downLoad/" + downFileName);                FileStream fileStream = new FileStream(save_path, FileMode.OpenOrCreate);                byte[] buffer = Convert.FromBase64String(downFileContent);                fileStream.Write(buffer, 0, buffer.Length);                fileStream.Close();                string path = "../Content1/downLoad/" + downFileName;                JsonRult result = new JsonRult();                result.Message = "";                result.Success = true;                var item = "<Table><Path>" + path + "</Path></Table>";                XmlDocument doc = new XmlDocument();                doc.LoadXml(item);                result.Value = Newtonsoft.Json.JsonConvert.SerializeObject(doc);                var rultResult = Json(result, JsonRequestBehavior.AllowGet);                return rultResult;            }            catch (Exception ex){                JsonRult result = new JsonRult();                result.Message = "";                result.Success = true;                var item = "<Table><Path>" + ex.Message + "</Path></Table>";                XmlDocument doc = new XmlDocument();                doc.LoadXml(item);                result.Value = Newtonsoft.Json.JsonConvert.SerializeObject(doc);                var rultResult = Json(result, JsonRequestBehavior.AllowGet);                return rultResult;            }        }