forked from eendrulat/map-and-app-gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadFile.ashx
More file actions
97 lines (87 loc) · 3.65 KB
/
UploadFile.ashx
File metadata and controls
97 lines (87 loc) · 3.65 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<%@ WebHandler Language="C#" Class="UploadFile" %>
/*
| Version 10.1.1
| Copyright 2012 Esri
|
| Licensed under the Apache License, Version 2.0 (the "License");
| you may not use this file except in compliance with the License.
| You may obtain a copy of the License at
|
| http://www.apache.org/licenses/LICENSE-2.0
|
| Unless required by applicable law or agreed to in writing, software
| distributed under the License is distributed on an "AS IS" BASIS,
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
| See the License for the specific language governing permissions and
| limitations under the License.
*/
using System;
using System.Web;
using System.IO;
public class UploadFile : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
// Upload the file
bool ok = false;
string filename = "";
string fileKey = "";
string uploader = "unknown";
if(0 < context.Request.Files.Count)
{
try
{
// From http://dojotoolkit.org/reference-guide/1.8/dojox/form/Uploader.html,
// Server Side Code PHP section
// [The server-side upload handler] is expecting one of four things:
// 1. A file or files from Flash (uploadedfilesFlash)
// 2. A file from HTML (uploadedfiles0)
// 3. Multiple files from HTML (uploadedfiles0, uploadedfiles1, etc)
// 4. A file array from HTML5 (uploadedfiles[])
// We have a simplification: we only upload a single file
fileKey = context.Request.Files.Keys[0];
uploader = "Form";
if(fileKey.EndsWith("s[]")) uploader = "HTML5";
else if(fileKey.EndsWith("Flash")) uploader = "Flash";
string filePath = context.Server.MapPath("Uploads");
filename = context.Request.Files[0].FileName;
if(0 < filename.Length)
{
// Timestamp the file so that it doesn't stomp on an existing file of the same name
// and so that we can provide a unique name back to the uploader
long timestamp = DateTime.UtcNow.Ticks;
filename = timestamp.ToString() + "_" + filename;
string fullFilename = filePath + "//" + filename;
context.Request.Files[0].SaveAs(fullFilename);
ok = true;
}
}
catch(Exception)
{
}
}
// Provide the expected response
// http://dojotoolkit.org/reference-guide/dojox/form/Uploader.html
// 4/26/13: HTML5 uploader not handling response; will force all uploads to use iframe in calling program
if("Flash" == uploader)
{
// Response to Flash uploader
context.Response.Write("file=" + filename + ",name=" + filename
+ ",uploader=" + uploader);
if(!ok) context.Response.Write(",error=Unable to save file");
}
else
{
// Response to HTML5/form uploader; latter needs to be bracketed by <textarea>
if("Form" == uploader) context.Response.Write("<textarea>");
context.Response.Write("[{'file':'" + filename + "','name':'" + filename
+ "','uploader':'" + uploader + "'"
+ (ok? "" : ",'error':'Unable to save file'" ) + "}]");
if("Form" == uploader) context.Response.Write("</textarea>");
}
}
public bool IsReusable {
get {
return false;
}
}
}