-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileUpload.ashx.cs
67 lines (57 loc) · 1.94 KB
/
FileUpload.ashx.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Alan.LargeFileUpload
{
/// <summary>
/// Summary description for FileUpload
/// </summary>
public class FileUpload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var req = context.Request;
var rep = context.Response;
req.ContentType = "application/json";
var fileName = req["fileName"];
if (String.IsNullOrWhiteSpace(fileName))
{
fileName = Guid.NewGuid().ToString();
}
using (System.IO.StreamReader reader = new System.IO.StreamReader(req.InputStream))
{
var base64 = reader.ReadToEnd();
var data = Convert.FromBase64String(base64);
var fileFullPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Static/" + fileName);
this.AppedOrCreate(fileFullPath, data);
}
rep.Write("{\"FileName\":\"" + fileName + "\"}");
}
public bool IsReusable
{
get
{
return false;
}
}
/// <summary>
/// 如果文件存在就附加数据, 如果不存在就新建文件
/// </summary>
/// <param name="filePath">文件绝对路径</param>
/// <param name="data">数据</param>
private void AppedOrCreate(string filePath, byte[] data)
{
if (!System.IO.File.Exists(filePath))
{
var fs = System.IO.File.Create(filePath);
fs.Close();
fs.Dispose();
}
using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Write))
{
fs.Write(data, 0, data.Length);
}
}
}
}