如今大部分項目都用的是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); } }
到這裏基本上主要方法都已經說完了。之後在其餘項目中使用時,直接建一個相關的配置文件便可。項目中添加一個引用便可。不須要再去實現相關功能。相對來講使用上有點麻煩。但解決了後期的很多問題。