最近自己做項目的時候需要添加一個功能,上傳使用者的圖檔,上傳使用者圖檔其實涉及到很多東西,不隻是一個html标簽或者asp.net封住好的FileUpload 控件,現在網站不再講究的是功能性,更多的是使用者體驗性,在這裡上傳圖檔就需要用到ajax無重新整理上傳圖檔,這裡面包含的東西不是一點半點。這裡用到的是一個插件swfupload 實作無重新整理上傳圖檔。直接上傳我的代碼供大家參考。
前台代碼區:
#draggable
{
width:50px;
height:50px;
padding:0.5em;
}
function uploadImgSuccess(file, response) {
//$("#imgAvatar").attr("src", response + "?ts=" + new Date());
//"url("+response + "?ts="+ new Date()+")")
var strs = $.parseJSON(response);
var imgPath = strs[0];
var imgWidth = strs[1];
var imgHeight = strs[2];
$("#avatarContainer").css("background-image", "url(" + imgPath + ")");
$("#avatarContainer").css("width", imgWidth + "px").css("height", imgHeight+"px");
};
$(function () {
var swfu;
swfu = new SWFUpload({
// Backend Settings
upload_url: "/Ajax/UploadAvatar.ashx",
post_params: {
"ASPSESSID": ""
},
// File Upload Settings
file_size_limit: "2 MB",
file_types: "*.jpg",
file_types_description: "JPG Images",
file_upload_limit: 0, // Zero means unlimited
// Event Handler Settings - these functions as defined in Handlers.js
// The handlers are not part of SWFUpload but are part of my website and control how
// my website reacts to the SWFUpload events.
swfupload_preload_handler: preLoad,
swfupload_load_failed_handler: loadFailed,
file_queue_error_handler: fileQueueError,
file_dialog_complete_handler: fileDialogComplete,
upload_progress_handler: uploadProgress,
upload_error_handler: uploadError,
upload_success_handler: uploadImgSuccess,
upload_complete_handler: uploadComplete,
// Button settings
button_image_url: "/JS/swf/images/XPButtonNoText_160x22.png",
button_placeholder_id: "btnUploadImgPlaceholder",
button_width: 160,
button_height: 22,
button_text: '選擇圖檔(最大2MB)',
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: "/JS/swf/swfupload.swf", // Relative to this file
flash9_url: "/JS/swf/swfupload_FP9.swf", // Relative to this file
custom_settings: {
upload_target: "divFileProgressContainer"
},
// Debug Settings
debug: false
});
});
$(function () {
$("#draggable").draggable({ containment: "parent" },
{ cursor: "crosshair" });
$("#draggable").dblclick(function () {
var thisOffset = $(this).offset();//擷取改容器的坐标位置
var parentOffset = $(this).parent().offset(); //擷取父容器的坐标位置
var left = thisOffset.left - parentOffset.left;//得到相對于父窗體的相對位置
var top = thisOffset.top - parentOffset.top; //得到相對于父窗體的相對位置
alert(left+" "+top);
});
});
拖過
背景一般處理程式區:
(UploadAvatar.ashx)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing;
using System.Web.Script.Serialization;
using System.Drawing.Drawing2D;
namespace NovelChannel.Ajax
{
///
/// UploadAvatar 的摘要說明
///
public class UploadAvatar : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
HttpPostedFile uploadFile = context.Request.Files["FileData"];
string ext = Path.GetExtension(uploadFile.FileName);
if (ext != ".jpg")
{
context.Response.Write("非法的檔案類型");
return;
}
string fileName = DateTime.Now.ToString("yyMMddhhMMss") + new Random().Next(1000, 9999) +".jpg";
string filePath = "/Images/UserImg/" + fileName;
string fullPath = HttpContext.Current.Server.MapPath("~" + filePath);
uploadFile.SaveAs(fullPath);
System.Drawing.Image img = Bitmap.FromFile(fullPath);
string[] strs={filePath,img.Size.Width.ToString(),img.Size.Height.ToString()};
JavaScriptSerializer jss=new JavaScriptSerializer ();
string json=jss.Serialize(strs);
context.Response.Write(json);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
這樣就可以實作無重新整理上傳圖檔的效果了。由于項目中包含一部分jQuery-UI的拖拽效果,如果對大家的項目沒有什麼幫助的話請适當删除。