在ASP.net Web表单中,并使用Blueimp jQuery文件Upload插件。
一切正常,但是当删除所选图像时,它会显示405方法不允许消息。
我也指定了deleteurl和deletetype,但它没有删除。
为此使用下面的处理程序代码。
public class GongosHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";//"application/json";
var r = new System.Collections.Generic.List<ViewDataUploadFilesResult>();
for (var x = 0; x < context.Request.Files.Count; x++)
{
HttpPostedFile hpf = context.Request.Files[x] as HttpPostedFile;
string FileName = string.Empty;
if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
{
string[] files = hpf.FileName.Split(new char[] { '\\' });
FileName = files[files.Length - 1];
}
else
{
FileName = hpf.FileName;
}
if (hpf.ContentLength == 0)
continue;
string savedFileName = context.Server.MapPath("~/images/" + FileName);
try
{
hpf.SaveAs(savedFileName);
}
catch (Exception ex)
{
}
var fileLength = new FileInfo(savedFileName).Length;
r.Add(new ViewDataUploadFilesResult()
{
thumbnailUrl = savedFileName,
name = FileName,
length = hpf.ContentLength,
type = hpf.ContentType,
url = string.Format("/images/{0}", FileName),
deleteUrl = string.Format("/images/{0}", FileName),
deleteType = "DELETE",
size = fileLength
});
var uploadedFiles = new
{
files = r.ToArray()
};
//was returning a new json string everytime, so then duplicating if
//sending multiple files. Example, file 1 was in first position,
//then file 1 & 2 in second position, and so on. So, I only grab,
//the last JSON instance to get all files.
if (x == (context.Request.Files.Count - 1))
{
var jsonObj = JsonConvert.SerializeObject(uploadedFiles, Formatting.Indented);
string jsonHttpOutputStream = jsonObj.ToString();
context.Response.Write(jsonHttpOutputStream);
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
public class ViewDataUploadFilesResult
{
public string thumbnailUrl { get; set; }
public string name { get; set; }
public int length { get; set; }
public string type { get; set; }
public string url { get; set; }
public string deleteUrl { get; set; }
public string deleteType { get; set; }
public long size { get; set; }
}
}