- 首先要把JqueryUI裏面的JS文件與CSS樣式給放到項目中的目錄裏面,建一個Web應用程序把下面的給放到<head>標籤裏面
- <link href="../Css/jquery-ui-1.8.2.custom.css" rel="stylesheet" type="text/css" />
- <link href="../Css/default.css" rel="stylesheet" type="text/css" />
- <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
- <script type="text/javascript" src="/SWFUpload/swfupload.js"></script>
- <script type="text/javascript" src="/SWFUpload/handlers.js"></script>
- <script src="js/jquery-1.7.2.js" type="text/javascript"></script>
- <script src="../SWFUpload/jquery-ui-1.8.2.custom.min.js" type="text/javascript"></script>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> : 這個是IE裏面的兼容問題,若是說是IE7以上的版本瀏覽器會在搜索框後面,有一個像被破壞的圖標,一點就會讓瀏覽器兼容Flash 控件了。 其他的依次引入就能夠了。
- <script type="text/javascript">
- var swfu;
- window.onload = function () {
- swfu = new SWFUpload({ //這個在Jquery文檔中有這個方法
-
//注意這是要請求的通常處理程序後面加了一個問號action=up。在後面會詳細說到的
-
upload_url: "/ashx/CutHeaderPhoto.ashx?action=up",
-
post_params: {
- "ASPSESSID": "<%=Session.SessionID %>"
- },
-
- file_size_limit: "2 MB", //說上傳的文件規定大小
- file_types: "*.jpg;*.gif", //上傳文件的格式
- file_types_description: "JPG Images", //上傳文件的類型
- file_upload_limit: 0,
-
-
-
- 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: ShowData,
- upload_complete_handler: uploadComplete,
-
- button_p_w_picpath_url: "/SWFUpload/p_w_picpaths/XPButtonNoText_160x22.png",
- button_placeholder_id: "spanButtonPlaceholder",
- button_width: 160,
- button_height: 22,
- button_text: '<span class="button">擇上傳的圖片<span class="buttonSmall">(2MB 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_url: "/SWFUpload/swfupload.swf",
- flash9_url: "/SWFUpload/swfupload_FP9.swf",
- custom_settings: {
- upload_target: "divFileProgressContainer"
- },
-
- debug: false
- });
- }
- //處理圖片上傳和截取圖片的通常處理程序,在前面說到了爲何要定義一個全局的 [var str] 當上傳圖片成功的
- //的時候會把圖片存到磁盤上,但是當截取的時候還還要取出圖片,當執行上傳圖片的時候,
string fileName = Path.GetFileName(file.FileName);這fileName爲當前上傳的圖片, 當截取圖片的時候,再一次執行這個通常處理程序fileName裏面這時就爲空了,因此取不到值。因此在前臺前 定義一個變量來保存上傳成功的圖片,當執行圖片截取的時候,fileName裏面就有了上次成功傳的圖片了。
- string action=context.Request["action"];
-
- if (action == "up")
- {
-
- HttpPostedFile file=context.Request.Files["Filedata"];
-
- string fileName = Path.GetFileName(file.FileName);
-
- string fileExt = Path.GetExtension(fileName);
-
- if (fileExt == ".jpg")
- {
-
- using (Image img = Image.FromStream(file.InputStream))
- {
-
- file.SaveAs(context.Server.MapPath("/UploadImages/"+fileName));
-
- context.Response.Write("ok:/UploadImages/"+fileName+":"+img.Width+":"+img.Height);
- }
- }
-
- else if (action == "cut"){
-
- int x=Convert.ToInt32(context.Request.Form["x"]);
- int y=Convert.ToInt32(context.Request.Form["y"]);
- int width=Convert.ToInt32(context.Request.Form["width"]);
- int height=Convert.ToInt32(context.Request.Form["height"]);
- string imgSrc = context.Request.Form["imgSrc"];
-
- using (Bitmap map =new Bitmap(width, height))
- {
-
- using (Graphics g = Graphics.FromImage(map))
- {
-
- using (Image img = Image.FromFile(context.Request.MapPath(imgSrc)))
- {
-
- g.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
-
- string newName = Guid.NewGuid().ToString().Substring(0,10);
- map.Save(context.Server.MapPath("/UploadImages/"+newName+".jpg"));
- context.Response.Write(context.Server.MapPath("/UploadImages/" + newName + ".jpg"));
- }
- }
- }
- }
- }
-
。 //上傳圖片
- var str;
-
- function ShowData(file, serverData) {
- str = serverData.split(":");
- if (str[0] == "ok") {
-
- $("#ImgContentId").css("backgroundImage", "url(" + str[1] + ")").css("width", str[2] + "px").css("height", str[3] + "px");
- }
- }
- //這裏是截取圖片的位置大小,
- $(function () {
-
- $("#CutPhotoId").draggable({ containment: 'parent' }).resizable({ containment: 'parent' });
-
- $("#btnCutPhotoId").click(function () {
-
- var y = $("#CutPhotoId").offset().top - $("#ImgContentId").offset().top;
- var x = $("#CutPhotoId").offset().left - $("#ImgContentId").offset().left;
- var width = $("#CutPhotoId").width();
- var height = $("#CutPhotoId").height();
-
- $.post("/ashx/CutHeaderPhoto.ashx", { "action": "cut", "x": parseInt(x), "y": parseInt(y), "width": parseInt(width), "height": parseInt(height), imgSrc: str[1] }, function (data) {
- $("#imgSrcId").attr("src",data);
- });
- });
- });
- <form id="form1" runat="server"> //這是表單
- <div id="content">
- <div id="swfu_container" style="margin: 0px 10px;">
- <div>
- <span id="spanButtonPlaceholder"></span>
- </div>
- <div id="divFileProgressContainer" style="height: 75px;">
- </div>
- <div id="ImgContentId" style="width: 300px; height: 300px">
- <div id="CutPhotoId" style="width: 100px; height: 100px; border: 1px solid red">
- </div>
- </div>
- <input type="button" value="截取頭像" id="btnCutPhotoId" />
- <img id="imgSrcId" alt="美圖"/>
- </div>
- </div>
- </form>