1
// According to http://msdn2.microsoft.com/en-us/library/system.web.httppostedfile.aspx
2
// "Files are uploaded in MIME multipart/form-data format.
3
// By default, all requests, including form fields and uploaded files,
4
// larger than 256 KB are buffered to disk, rather than held in server memory."
5
// So we can use an HttpHandler to handle uploaded files and not have to worry
6
// about the server recycling the request do to low memory.
7
// don't forget to increase the MaxRequestLength in the web.config.
8
// If you server is still giving errors, then something else is wrong.
9
// I've uploaded a 1.3 gig file without any problems. One thing to note,
10
// when the SaveAs function is called, it takes time for the server to
11
// save the file. The larger the file, the longer it takes.
12
// So if a progress bar is used in the upload, it may read 100%, but the upload won't
13
// be complete until the file is saved. So it may look like it is stalled, but it
14
// is not.
15
16
//该源码下载自www.51aspx.com(51aspx.com)
17
//5_1_a_s_p_x.c_o_m
18
19
using System;
20
using System.Data;
21
using System.Configuration;
22
using System.Web;
23
using System.Web.Security;
24
using System.Web.UI;
25
using System.Web.UI.WebControls;
26
using System.Web.UI.WebControls.WebParts;
27
using System.Web.UI.HtmlControls;
28
using System.IO;
29
30
/**//// <summary>
31
/// Upload handler for uploading files.
32
/// </summary>
33
public class Upload : IHttpHandler
34
...{
35
public Upload()
36
...{
37
}
38
39
IHttpHandler Members#region IHttpHandler Members
40
41
public bool IsReusable
42
...{
43
get ...{ return true; }
44
}
45
46
public void ProcessRequest(HttpContext context)
47
...{
48
// Example of using a passed in value in the query string to set a categoryId
49
// Now you can do anything you need to witht the file.
50
//int categoryId = 0;
51
//if (context.Request.QueryString["CategoryID"] != null)
52
//{
53
// try
54
// {
55
// categoryId = Convert.ToInt32(context.Request.QueryString["CategoryID"]);
56
// }
57
// catch (Exception err)
58
// {
59
// categoryId = 0;
60
// }
61
//}
62
//if (categoryId > 0)
63
//{
64
//}
65
66
if (context.Request.Files.Count > 0)
67
...{
68
// get the applications path
69
string tempFile = context.Request.PhysicalApplicationPath;
70
// loop through all the uploaded files
71
for(int j = 0; j < context.Request.Files.Count; j++)
72
...{
73
// get the current file
74
HttpPostedFile uploadFile = context.Request.Files[j];
75
// if there was a file uploded
76
if (uploadFile.ContentLength > 0)
77
...{
78
// save the file to the upload directory
79
80
//use this if testing from a classic style upload, ie.
81
82
// <form action="Upload.axd" method="post" enctype="multipart/form-data">
83
// <input type="file" name="fileUpload" />
84
// <input type="submit" value="Upload" />
85
//</form>
86
87
// this is because flash sends just the filename, where the above
88
//will send the file path, ie. c:\My Pictures\test1.jpg
89
//you can use Test.thm to test this page.
90
//string filename = uploadFile.FileName.Substring(uploadFile.FileName.LastIndexOf("\\"));
91
//uploadFile.SaveAs(string.Format("{0}{1}{2}", tempFile, "Upload\\", filename));
92
93
// use this if using flash to upload
94
uploadFile.SaveAs(string.Format("{0}{1}{2}", tempFile, "Upload\\", uploadFile.FileName));
95
96
// HttpPostedFile has an InputStream also. You can pass this to
97
// a function, or business logic. You can save it a database:
98
99
//byte[] fileData = new byte[uploadFile.ContentLength];
100
//uploadFile.InputStream.Write(fileData, 0, fileData.Length);
101
// save byte array into database.
102
103
// something I do is extract files from a zip file by passing
104
// the inputStream to a function that uses SharpZipLib found here:
105
// http://www.icsharpcode.net/OpenSource/SharpZipLib/
106
// and then save the files to disk.
107
}
108
}
109
}
110
// Used as a fix for a bug in mac flash player that makes the
111
// onComplete event not fire
112
HttpContext.Current.Response.Write(" ");
113
}
114
115
#endregion
116
}
117