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

现在大部分项目都用的是mvc4.0做的开发。所以相关代码也是使用mvc做演示。调用模式与之前相同使用iframe。

 

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

2、为我们的flash做一个简单的调用页面。看下与之前的参数:

flash上传:

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

通用上传:

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

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

 

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:

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

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);
    }
}

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

posted @ 2013-12-17 11:18  班然  阅读(873)  评论(0编辑  收藏  举报