多项目上传文件解决方案之:Flash插件使用

如今大部分项目都用的是mvc4.0作的开发。因此相关代码也是使用mvc作演示。调用模式与以前相同使用iframe。mvc

 

一、首先咱们须要添加xml配置文件。放在根目录下。upload.xml配置文件。在flash插件开发中提到过的。app

二、为咱们的flash作一个简单的调用页面。看下与以前的参数:url

flash上传:spa

public ActionResult FlashUpload(string type, string ParentFileName, string ParentImgSrc)插件

通用上传:code

public ActionResult Upload(string ParentImgSrc, string ParentImgInput, string ImageWidth, string ImageHeight, string FileName, int? IsCompletePath, string UploadPath, string FileExt, string FileLength)xml

参数减小了。这也是这个解决方案的主要目的之一。blog

 

Controllers:图片

 

/// <summary>
/// 使用flash上传
/// </summary>
/// <param name="type"></param>
/// <param name="ParentFileName">上传完成后存储路径的input</param>
/// <param name="ParentImgSrc">显示图片的img标签id</param>
/// <returns></returns>
public ActionResult FlashUpload(string type,string ParentFileName, string ParentImgSrc)
{
    ViewBag.type = type;
    ViewBag.ParentFileName = ParentFileName;
    ViewBag.ParentImgSrc = ParentImgSrc;
    return View();
}

Views:ip

为了将本地上传的文件与正式上传的文件区分开。在视图里作了一个简单的处理。

string appconfig = "upload",
    baseurl = "http://192.168.2.100/upload/",
    ip = IPHelper.getRealIPAddress,
    type = ViewBag.type as string,
    fileName = ViewBag.ParentFileName as string,
    imgId = ViewBag.ParentImgSrc as string;
    if (ip.Equals("127.0.0.1") || ip.StartsWith("192.168.")|| ip.Equals("::1"))//本地使用本地配置文件
    {
        appconfig = "locationupload";
    }

flash调用代码:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="400" height="30" id="Upload">
            <param name="movie" value="@(baseurl)Upload.swf?appconfig=@(appconfig)&type=@(type)&jsfun=funUploadSucc" />
            <param name="quality" value="high" />
            <param name="bgcolor" value="#ffffff" />
            <param name="allowScriptAccess" value="always" />
            <param name="allowFullScreen" value="true" />
            <!--[if !IE]>-->
            <object type="application/x-shockwave-flash" data="@(baseurl)Upload.swf?appconfig=@(appconfig)&type=@(type)&jsfun=funUploadSucc" width="400" height="30">
                <param name="quality" value="high" />
                <param name="bgcolor" value="#ffffff" />
                <param name="allowScriptAccess" value="always" />
                <param name="allowFullScreen" value="true" />
                <!--<![endif]-->
                <!--[if gte IE 6]>-->
                <p>
                    Either scripts and active content are not permitted to run or Adobe Flash Player version
                        11.1.0 or greater is not installed.
                </p>
                <!--<![endif]-->
                <a href="http://www.adobe.com/go/getflashplayer">
                    <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player" />
                </a>
                <!--[if !IE]>-->
            </object>
            <!--<![endif]-->
        </object>

flash回调方法:funUploadSucc

var $p = function (id) {
    return window.parent.document.getElementById(id);
};
function funUploadSucc(id, path) {
    var pid = '@(fileId)',
        pname = '@(fileName)',
        img = '@(imgId)';
    if (pid.length > 0 && $p(pid)) {//文件id
        $p(pid).value = id;
    }
    if (pname.length > 0 && $p(pname)) {//文件路径
        $p(pname).value = path;
    }
    if (img.length > 0 && $p(img)) {//若是有img标签,则显示图片
        $p(img).setAttribute('src', path);
    }
}

到这里基本上主要方法都已经说完了。之后在其余项目中使用时,直接建一个相关的配置文件便可。项目中添加一个引用便可。不须要再去实现相关功能。相对来讲使用上有点麻烦。但解决了后期的很多问题。

相关文章
相关标签/搜索