MVC 上傳檔案+預覽

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   sp   

學習記錄    複慣用

一、調用了Uploadify外掛程式

Uploadify是JQuery的一個上傳外掛程式,實現的效果非常不錯,帶進度顯示。(具體參數百度就是。)

二、上傳檔案的方法

namespace PM.Myjobweb.Controllers{    public class FileOperationsController : Controller    {        BLL.pc_attachment bll_attachment = new BLL.pc_attachment();        //        // GET: /FileOperations/        /// <summary>        /// 上傳檔案        /// </summary>        /// <param name="fileData"></param>        /// <returns></returns>        [AcceptVerbs(HttpVerbs.Post)]        public ActionResult Upload(HttpPostedFileBase fileData)        {            if (fileData != null)            {                try                {                    // 檔案上傳後的儲存路徑                    string filePath = Server.MapPath(Request["folder"] + "\\");                    if (!Directory.Exists(filePath))                    {                        Directory.CreateDirectory(filePath);                    }                    string fileName = Path.GetFileName(fileData.FileName);// 原始檔案名稱                    string fileExtension = Path.GetExtension(fileName); // 副檔名                    string saveName = Guid.NewGuid().ToString() + fileExtension; // 儲存檔案名稱                    string url = "/Content/JqueryUpload/upload/" + saveName;                    fileData.SaveAs(filePath + saveName);                    if (fileExtension == ".docx" || fileExtension == ".doc" || fileExtension == ".pdf" || fileExtension == ".xlsx")                        url = "/Content/JqueryUpload/html/" + PM.Common.FilesToHtml.FileToHtml(filePath + saveName, TimeParser.ConvertDateTimeInt(DateTime.Now).ToString(), fileExtension.Substring(1));                    FilesModel m_files = new FilesModel();                    m_files.DesName = fileName;                    m_files.FileExtension = fileExtension;                    m_files.SaveName = saveName;                    m_files.Att_Expansion2 = url;                    Model.pc_attachment m_attachment = new Model.pc_attachment();                    m_attachment.Att_Name = fileName;                    m_attachment.Att_Type = fileExtension;                    m_attachment.Att_Url = saveName;                    m_attachment.Att_Expansion1 = DateTime.Now.ToString();                    m_attachment.Att_Expansion2 = url;                    int resultId = bll_attachment.Add(m_attachment);                    if (resultId > 0)                    {                        m_files.ResultId = resultId;                        return Json(new { code = 0, msg = m_files });                    }                    else                    {                        return Json(new { code = -1, msg = m_files });                    }                }                catch (Exception ex)                {                    return Json(new { code = -1, Message = ex.Message }, JsonRequestBehavior.AllowGet);                }            }            else            {                return Json(new { code = false, Message = "請選擇要上傳的檔案!" }, JsonRequestBehavior.AllowGet);            }        }    }    public class FilesModel    {        public int ResultId { get; set; }        public string FileExtension { get; set; }        public string SaveName { get; set; }        public string DesName { get; set; }        public string Att_Expansion2 { get; set; }    }}
上傳檔案的方法

三、JS

<script type="text/javascript">    $(document).ready(function () {        $("#uploadify").uploadify({            ‘uploader‘: ‘/Content/JqueryUpload/js/uploadify.swf‘,            ‘script‘: ‘/FileOperations/Upload‘,            ‘cancelImg‘: ‘/Content/JqueryUpload/js/cancel.png‘,            ‘buttonText‘: ‘upload‘,            ‘folder‘: ‘/Content/JqueryUpload/upload‘,            ‘queueID‘: ‘fileQueue‘,//            ‘hideButton‘:true,            ‘multi‘: true,            ‘auto‘: true,            //‘wmode‘: ‘transparent‘,            ‘onComplete‘: function (event, queueId, fileObj, response, data) {                var obj = eval("(" + response + ")");                var oldName = ‘‘;                var newName = ‘‘;                var fileType = ‘‘;                var Att_Expansion2 = ‘‘;                var id = ‘‘;                switch (obj.msg.FileExtension) {                    case ‘.docx‘:                        fileType = ‘icon-word‘;                        break;                    case ‘.doc‘:                        fileType = ‘icon-word‘;                        break;                    case ‘.pdf‘:                        fileType = ‘icon-pdf‘;                        break;                    case ‘.xlsx‘:                        fileType = ‘icon-excel‘;                        break;                    case ‘.jpg‘:                        fileType = ‘icon-tupian‘;                        break;                    case ‘.ppt‘:                        fileType = ‘icon-ppt‘;                        break;                    case ‘.txt‘:                        fileType = ‘icon-txt‘;                        break;                }                oldName = obj.msg.DesName;                newName = obj.msg.SaveName;                id = obj.msg.ResultId;                Att_Expansion2 = obj.msg.Att_Expansion2;                var liobj = ‘<li id="add" tag="‘ + id + ‘"><i class="icon-word"></i>"‘ + oldName + ‘"<a target="_blank" href=‘ + Att_Expansion2 + ‘>預覽</a><a href="/Content/JqueryUpload/upload/‘ + newName + ‘">下載</a></li>‘;                $(liobj).appendTo($(‘.annex-list‘));            }        });    });
JS

四、調用

<label>附件</label>            <ul class="annex-list">                <li>                     <input type="file" name="uploadify" id="uploadify" style=" visibility:hidden" /><div id="fileQueue">                    </div>                </li>            </ul>
調用

 

MVC 上傳檔案+預覽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.