这两天在做图片上传并显示的功能,之前就用过swfupload,觉得很不错,之前是用asp.net webform做的,这次的项目是用asp.net MVC3来做,视图引擎用的是Razor。
将js文件引入到项目中后发现,上传图片的按钮不显示,于是乎求助于搜索引擎,发现很多人都有这个问题。很多人给出的解决方案都是将flash_url的路径改为以@开头的。
我照做,结果还是不行,就这个问题磨蹭了很长时间。在不经意间发现了<%=Session.SessionID%>.是不是这个问题呢?
于是将这个改为@Session.SessionID,按钮可以显示了,我这个项目的问题就是出在不同的视图引擎,支持的语言不一样。
在Razor的视图引擎中,服务端代码不要出现<%%>.希望可以帮到大家。
<script src="@Url.Content("~/Content/swfupload/swfupload.js")" type="text/javascript"></script> <script src="@Url.Content("~/Content/swfupload/handlers.js")" type="text/javascript"></script> <script type="text/javascript"> var swfu; $(function () { swfu = new SWFUpload({ //Backend Settings upload_url: "/Code/UploadNewsImg.ashx", post_params: { "ASPSESSID": "@Session.SessionID" }, // File Upload Settings file_size_limit: "2 MB", file_types: "*.jpg;*.gif;*.png", file_types_description: "JPG Images", file_upload_limit: "0", // Zero means unlimited file_queue_error_handler: fileQueueError, file_dialog_complete_handler: fileDialogComplete, upload_progress_handler: uploadProgress, upload_error_handler: uploadError, upload_success_handler: function (file, serverData) { var fields = serverData.split(":"); if (fields[0] == "ok") { // Get the editor instance that we want to interact with. $("#perimage").attr("src", fields[1]); // Check the active editing mode. //alert(fields[1]); } else if(fields[0]=="nook") { alert("图片必须小于50kb"); } else if (fields[0] == "error") {//良好的编程素养!《一个程序员的自我修养》 alert("上传失败" + fields[1]); } else { alert("未知的服务端返回"); } }, upload_complete_handler: uploadComplete, // Button settings button_image_url: '@Url.Content("~/Content/swfupload/images/XPButtonNoText_160x22.png")', button_placeholder_id: "spanButtonPlaceholder", button_ 160, button_height: 22, button_text: '<span class="button">选择图片<span class="buttonSmall">(2 MB Max)</span></span>', button_text_style: '.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }', button_text_top_padding: 1, button_text_left_padding: 5, //Flash Settings flash_url: '@Url.Content("~/Content/swfupload/swfupload.swf")', // Relative to this file flash9_url: '@Url.Content("~/Content/swfupload/swfupload_FP9.swf")', // Relative to this file custom_settings: { upload_target: "divFileProgressContainer" }, //Debug Settings debug: false }); }); function SF_DEL(){ $.post("/Code/DelImg.ashx", { userid: $("#UserId").val() }, function (data) { if(data=="OK") { $("#perimage").attr("src", "/Content/images/defaultPhoto.gif"); } }, "text"); } </script>