• uploadify 上传


    本来想做一套上传公用的组建的,里面包含文件转码等功能,看来这些都只能后来一步一步加上了,先写下来。。。

    1,引入脚本等

    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <title>Index</title>
        <link href="/uploadify/uploadify.css" rel="stylesheet" />
    </head>
    <body>
        <input type="file" name="upload" id="upload" />
    </body>
    </html>
    <script src="/Scripts/jquery-1.11.1.min.js"></script>
    <script src="/uploadify/jquery.uploadify.min.js"></script>
    <script>
        $(function () {
            $('#upload').uploadify({
                'formData': { 'folder': 'd:\' },
                'buttonText': '选择文件',
                'buttonClass': 'browser',
                'removeCompleted': false,
                'swf': '/uploadify/uploadify.swf',
                'uploader': '/FileUp/Upload',
                'fileSizeLimit':'500MB',
                'onError': function (event, id, fileObj, errorObj) {
                    if (errorObj.type === "File Size") {
                        alert('超过文件上传大小限制(2M)!');
                        return;
                    }
                    alert(errorObj.type + ', Error: ' + errorObj.info);
                },
            });
        });
    </script>
    

    2,后台上传代码

     public class FileUpController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
    
            public ContentResult Upload(HttpPostedFileBase fileData, string folder)
            {
                string filename = "";
                if (null != fileData)
                {
                    var length = fileData.ContentLength;
                    try
                    {
                        filename = Path.GetFileName(fileData.FileName); //获得文件名
                        saveFile(fileData, folder, filename);
                    }
                    catch (Exception ex)
                    {
                        filename = ex.ToString();
                    }
                }
                return Content(filename);
            }
    
            [NonAction]
            private bool saveFile(HttpPostedFileBase postedFile, string filepath, string saveName)
            {
                bool result = false;
                if (!Directory.Exists(filepath))
                {
                    Directory.CreateDirectory(filepath);
                }
                try
                {
                    postedFile.SaveAs(Path.Combine(filepath, saveName));
                    result = true;
                }
                catch (Exception e)
                {
                    throw new ApplicationException(e.Message);
                }
                return result;
            }
        }
    

    3,设置web.config(在system.web节点中)

        <httpRuntime requestLengthDiskThreshold="256" maxRequestLength="2097151"/>   //假如超过256kb文件,其将缓存到硬盘
    

      

  • 相关阅读:
    [codevs 1227] 方格取数 2
    记冬令营
    Codeforces Round 558(Div 2)题解
    Educational Round 64 题解
    [GXOI/GZOI2019]与或和(位运算,单调栈)
    LOJ6053 简单的函数(min_25筛)
    LOJ6235 区间素数个数(min_25筛)
    min_25筛学习笔记
    CF1142C U2(计算几何,凸包)
    关于一些没做出来的SBCF题
  • 原文地址:https://www.cnblogs.com/objectboy/p/4009587.html
Copyright © 2020-2023  润新知