asp.net使用jquery.uploadifyv2.1.4 上传文件 GLenn

先引用jquery

View Code
1     <script src="Scripts/jquery.js" type="text/javascript"></script>
2     <script src="Scripts/swfobject.js" type="text/javascript"></script>
3     <link href="Scripts/jquery.uploadify-v2.1.4/uploadify.css" rel="stylesheet" type="text/css" />
4     <script src="Scripts/jquery.uploadify-v2.1.4/jquery.uploadify.v2.1.4.js" type="text/javascript"></script>

然后写配置

View Code

 然后创建”UpLoad.ashx“文件用于上传文件

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Web;
 4 using System.IO;
 5 
 6 namespace UpLoad
 7 {
 8     /// <summary>
 9     /// DEARBOY.NET
10     /// 张雪雷 2011-11-21 23:35:50
11     /// QQ:506691116
12     /// </summary>
13     public class UpLoad : IHttpHandler
14     {
15         public void ProcessRequest(HttpContext context)
16         {
17             context.Response.ContentType = "text/plain";
18             context.Response.Charset = "utf-8";
19             //获取上传文件队列  
20             HttpPostedFile oFile = context.Request.Files["Filedata"];
21             if (oFile != null)
22             {
23                 string topDir = context.Request["folder"];  // 获取uploadify的folder配置,在此示例中,客户端配置了上传到 Files/ 文件夹
24                 // 检测并创建目录:当月上传的文件放到以当月命名的文件夹中,例如2011年11月的文件放到网站根目录下的 /Files/201111 里面
25                 string dateFolder = HttpContext.Current.Server.MapPath(topDir) + "\\" + DateTime.Now.Date.ToString("yyyyMM");
26                 if (!Directory.Exists(dateFolder))  // 检测是否存在磁盘目录
27                 {
28                     Directory.CreateDirectory(dateFolder);  // 不存在的情况下,创建这个文件目录 例如 C:/wwwroot/Files/201111/
29                 }
30 
31                 // 使用Guid命名文件,确保每次文件名不会重复
32                 string guidFileName=Guid.NewGuid() + Path.GetExtension(oFile.FileName).ToLower();
33 
34                 // 保存文件,注意这个可是完整路径,例如C:/wwwroot/Files/201111/92b2ce5b-88af-405e-8262-d04b552f48cf.jpg
35                 oFile.SaveAs(dateFolder + "\\" + guidFileName);
36 
37                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
38                 //////// TODO 在此,您可以添加自己的业务逻辑,比如保存这个文件信息到数据库
39                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
40 
41                 // 上面的所有操作顺利完成,你就完成了一个文件的上传(和保存信息到数据库),返回成功,在此我返回1,表示上传了一个文件
42                 context.Response.Write("1");
43             }
44             else
45             {
46                 context.Response.Write("0");
47             }
48         }
49 
50         public bool IsReusable
51         {
52             get
53             {
54                 return false;
55             }
56         }
57     }
58 }

遇到的问题:

1.在upload.ashx中不能获取session。

解决方法:

2.不能上传名字为中文名文件。

解决方法:

因为flash 是uft-8的编码 ,而网站是gb2312的。 所以上传的时候乱码了。

View Code
1  context.Response.ContentType = "text/plain";
2             context.Request.ContentEncoding = Encoding.GetEncoding("UTF-8");
3 
4             context.Response.ContentEncoding = Encoding.GetEncoding("UTF-8");
5             context.Response.Charset = "UTF-8";
原文地址:https://www.cnblogs.com/cuiguangzhi/p/2803076.html