最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

swfupload ajax無(wú)刷新上傳圖片實(shí)例代碼

 更新時(shí)間:2013年06月16日 15:04:55   作者:  
在這里上傳圖片就需要用到ajax無(wú)刷新上傳圖片,這里面包含的東西不是一點(diǎn)半點(diǎn)。這里用到的是一個(gè)插件swfupload實(shí)現(xiàn)無(wú)刷新上傳圖片,感興趣的朋友可以參考下哈
最近自己做項(xiàng)目的時(shí)候需要添加一個(gè)功能,上傳用戶(hù)的圖片,上傳用戶(hù)圖片其實(shí)涉及到很多東西,不只是一個(gè)html標(biāo)簽<input id="File1" type="file" />或者asp.net封住好的FileUpload 控件,現(xiàn)在網(wǎng)站不再講究的是功能性,更多的是用戶(hù)體驗(yàn)性,在這里上傳圖片就需要用到ajax無(wú)刷新上傳圖片,這里面包含的東西不是一點(diǎn)半點(diǎn)。這里用到的是一個(gè)插件swfupload 實(shí)現(xiàn)無(wú)刷新上傳圖片。直接上傳我的代碼供大家參考。

前臺(tái)代碼區(qū):
復(fù)制代碼 代碼如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChangeAvatar.aspx.cs" Inherits="NovelChannel.ChangeAvatar" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="/CSS/jQueryUI/jquery-ui-1.10.2.custom.css" rel="stylesheet" type="text/css" />
<style type="text/css">
#draggable
{
width:50px;
height:50px;
padding:0.5em;
}
</style>
<script src="/JS/jQuery/jquery-1.9.1.js" type="text/javascript"></script>
<script src="/JS/jQuery/jquery-ui-1.10.2.custom.js" type="text/javascript"></script>
<script type="text/javascript" src="/JS/swf/swfupload.js"></script>
<script type="text/javascript" src="/JS/swf/handlers.js"></script>
<script type="text/javascript">
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": "<%=Session.SessionID %>"
},
// 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: '<span class="button">選擇圖片(最大2MB)</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: "/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();//獲取改容器的坐標(biāo)位置
var parentOffset = $(this).parent().offset(); //獲取父容器的坐標(biāo)位置
var left = thisOffset.left - parentOffset.left;//得到相對(duì)于父窗體的相對(duì)位置
var top = thisOffset.top - parentOffset.top; //得到相對(duì)于父窗體的相對(duì)位置
alert(left+" "+top);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<span id="btnUploadImgPlaceholder"></span>
<div id="divFileProgressContainer"></div>
<br />
<div id="avatarContainer" style="width:200px;height:300px">
<div id="draggable" style="background-color:transparent;border-width:1px;border-color:Black;border-style:solid;">
拖過(guò)
</div>
</div>
<img id="imgAvatar" style="display:none;"/>
</div>
</form>
</body>
</html>

后臺(tái)一般處理程序區(qū):
(UploadAvatar.ashx)
復(fù)制代碼 代碼如下:

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
{
/// <summary>
/// UploadAvatar 的摘要說(shuō)明
/// </summary>
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("非法的文件類(lèi)型");
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;
}
}
}
}

這樣就可以實(shí)現(xiàn)無(wú)刷新上傳圖片的效果了。由于項(xiàng)目中包含一部分jQuery-UI的拖拽效果,如果對(duì)大家的項(xiàng)目沒(méi)有什么幫助的話(huà)請(qǐng)適當(dāng)刪除。

相關(guān)文章

  • Request.RawUrl 屬性的應(yīng)用收

    Request.RawUrl 屬性的應(yīng)用收

    用來(lái)對(duì)字符串分析,有選擇的導(dǎo)向。
    2009-06-06
  • 垃圾代碼二三行 ASPX小馬

    垃圾代碼二三行 ASPX小馬

    垃圾代碼二三行 ASPX小馬...
    2007-12-12
  • 在ASP.Net Core中使用Lamar的全過(guò)程

    在ASP.Net Core中使用Lamar的全過(guò)程

    這篇文章主要給大家介紹了關(guān)于在ASP.Net Core中使用Lamar的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 如何利用HttpClientFactory實(shí)現(xiàn)簡(jiǎn)單的熔斷降級(jí)

    如何利用HttpClientFactory實(shí)現(xiàn)簡(jiǎn)單的熔斷降級(jí)

    這篇文章主要給大家介紹了關(guān)于如何利用HttpClientFactory實(shí)現(xiàn)簡(jiǎn)單的熔斷降級(jí)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • Asp.net 在三層架構(gòu)中事務(wù)的使用實(shí)例代碼

    Asp.net 在三層架構(gòu)中事務(wù)的使用實(shí)例代碼

    再我知道的.net中,執(zhí)行事務(wù)有兩種方法,一種是自動(dòng)事物,使用起來(lái)相當(dāng)簡(jiǎn)單,但是需要配置服務(wù)器,如果你用的是web共享主機(jī),即沒(méi)有權(quán)限配置自己的服務(wù)器,那就沒(méi)必要用這種了,接下了我講下.net中最普通的事務(wù)SqlTransaction
    2012-10-10
  • Asp.net 菜單控件簡(jiǎn)潔版

    Asp.net 菜單控件簡(jiǎn)潔版

    asp.net自帶的菜單控件采用的table和javascript,導(dǎo)致生成的大量的html,同時(shí)在很多瀏覽器中都無(wú)法顯示出子菜單,也只能在IE中能顯示出來(lái)。
    2009-11-11
  • 建立自定義的數(shù)據(jù)驅(qū)動(dòng)的本地化資源provider

    建立自定義的數(shù)據(jù)驅(qū)動(dòng)的本地化資源provider

    本文探討了自定義的本地化資源提供者.如果想用一個(gè)可替代系統(tǒng)的資源處理方案,例如把所有的資源放入數(shù)據(jù)庫(kù)中,而不是放在分散的資源文件里,你可以自定義一個(gè)resource provider.
    2010-06-06
  • asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)的方法

    asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)的方法

    這篇文章主要介紹了asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)的方法,實(shí)現(xiàn)讀取excel數(shù)據(jù)并導(dǎo)入到SQL Server數(shù)據(jù)庫(kù)的功能,是非常實(shí)用的技巧,需要的朋友可以參考下
    2015-01-01
  • ASP.NET泛型一之泛型簡(jiǎn)介與基本語(yǔ)法

    ASP.NET泛型一之泛型簡(jiǎn)介與基本語(yǔ)法

    這篇文章介紹了ASP.NET泛型的簡(jiǎn)介與基本語(yǔ)法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • vs2010顯示代碼行數(shù)的方法

    vs2010顯示代碼行數(shù)的方法

    這篇文章主要為大家詳細(xì)介紹了如何在vs2010中顯示代碼的行數(shù),僅要三步實(shí)現(xiàn)代碼行數(shù)顯示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11

最新評(píng)論

民勤县| 西乡县| 辽阳市| 阜城县| 大荔县| 富阳市| 博客| 伽师县| 太白县| 福贡县| 开阳县| 潞西市| 崇州市| 宣城市| 宁蒗| 炎陵县| 吉木萨尔县| 宾阳县| 浦江县| 吴江市| 徐汇区| 板桥市| 施秉县| 安化县| 赣榆县| 宜宾县| 阳春市| 通化市| 宁城县| 闻喜县| 股票| 丹巴县| 菏泽市| 宝清县| 自贡市| 郁南县| 富阳市| 垣曲县| 台东市| 凭祥市| 巩义市|