[Asp.net] 비동기 파일 업로드 Ajax File Upload
출처 : http://hellogk.tistory.com/28
출처2(jquery.form.js) : https://github.com/malsup/form/
원출처 분께서 설명을 너무 잘 해주신 관계로
파일 업로드 부분과 소스만 올립니다.
web.config 추가할 내용
<system.web>
<!-- 업로드 용량 및 시간 늘리기 -->
<httpRuntime executionTimeout="3600" maxRequestLength="2097151" />
</system.web>
FileUploadHandler.ashx 내용
<%@ WebHandler Language="C#" Class="FileUploadHandler" %>
using System;
using System.Web;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
public class FileUploadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string rtn = string.Empty;
string path = "~/data/";
string _overWrite = context.Request.Form["OverWrite"] ?? "false";
bool overWrite = bool.Parse(_overWrite.Trim());
if (context.Request.Files.Count > 0)
{
HttpFileCollection files = context.Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
string serverPath = context.Server.MapPath(path);
string fname = file.FileName;
if (!overWrite)
{
int index = 1;
while (File.Exists(serverPath + fname))
{
fname = string.Format("{0}[{1}]{2}",
Regex.Replace(Path.GetFileNameWithoutExtension(fname), @"\[(\d+)\]", string.Empty),
index.ToString(), Path.GetExtension(fname));
index++;
}
}
file.SaveAs(serverPath + fname);
rtn = fname;
}
context.Response.ContentType = "text/plain";
context.Response.Write(rtn);
}
else
{
context.Response.ContentType = "text/plain";
context.Response.Write("Error!");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}