图书商城项目-二

知识点:线程内唯一对象,其他类中使用session,SwfUpLoad以及selectImage插件的使用

图书商城项目-二

知识点:线程内唯一对象,其他类中使用session,SwfUpLoad以及selectImage插件的使用

线程内唯一对象

举个例子,比如我请求一个default.aspx页面,代码执行到事件管道的第11和12之间执行一个方法,这个方法开一个线程去执行某些逻辑,这个协程开启的方法会用到HttpContext对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
protected void Page_Load(object sender, EventArgs e)
{
ParameterizedThreadStart par = new
ParameterizedThreadStart(GetFilePath);
Thread thread1 = new Thread(par);
thread1.IsBackground = true;
thread1.Start(HttpContext.Current);
//GetFilePath();

}

protected void GetFilePath(object context)
{
HttpContext context = (HttpContext)context;
}

但是执行会发现报错:context为null,这是因为跨线程的HttpContext并不包含请求数据,但是如果你不开线程直接调用的话是没有问题的,因为这就相当于主线程调用。

要在其他类中使用session必须实现接口System.Web.SessionState.IRequiresSessionState

SwfUpLoad的使用

首先导入所需的文件,做好相关js的引用,下面是一个小demo测试,JS部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<script type="text/javascript">
var swfu;
window.onload = function () {
swfu = new SWFUpload({
// Backend Settings
upload_url: "/ashx/upload.ashx",//请求的页面
post_params: {
"ASPSESSID": "<%=Session.SessionID %>"
},

// File Upload Settings //配置,文件的格式大小等等
file_size_limit: "2 MB",
file_types: "*.jpg;*.gif",
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: showImage,
upload_complete_handler: uploadComplete,

// Button settings
button_image_url: "/SWFUpload/images/XPButtonNoText_160x22.png",
button_placeholder_id: "spanButtonPlaceholder",
button_width: 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的按钮的路径
flash_url: "/SWFUpload/swfupload.swf", // Relative to this file
flash9_url: "/SWFUpload/swfupload_FP9.swf", // Relative to this file

custom_settings: {
upload_target: "divFileProgressContainer"//上传信息显示的区域
},

// Debug Settings
debug: false
});
}
//上传成功以后调用该方法(改写的方法)
function showImage(file, serverData) {//serverData即服务器返回的数据
//$("#showPhoto").attr("src", serverData);
$("#showPhoto").attr("src",serverData);
}
</script>

Html部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<div id="content">
<div id="swfu_container" style="margin: 0px 10px;">
<div>
aaaaaaa
<span id="spanButtonPlaceholder"></span>
aaaaaa
</div>
ssssssssssssss
<div id="divFileProgressContainer" style="height: 75px;"></div>
sssssssssss
<div id="thumbnails"></div>
<img id="showPhoto"></img>
</div>
</div>

服务器处理的aspx要注意,服务器返回图片的地址不能是物理路径,只有保存的时候需要物理路径

imgAreaSelect插件实现图片裁剪

剪裁示例:https://www.cnblogs.com/mizzle/archive/2011/10/13/2209891.html


图书商城项目-二
http://www.kju147.com/2018/02/09/图书商城项目-二/
作者
个人学习
发布于
2018年2月9日
许可协议