使用 visual studio 网络表单中的通用处理程序拖放上传文件
Drag and Drop upload file by using Generic Handler in visual studio webform
当我尝试将文件(拖放)传递给 Generic Handler 时,Generic Handler 中的 context.Request.Files.Count 包含 0,我已经尝试了很多方法,但仍然无法解决。
通用处理程序:
CommonFunction _commonFunction = new CommonFunction();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
HttpFileCollection files = context.Request.Files;
string dhidStr = context.Request["dhid"];
int dhid = Convert.ToInt32(dhidStr);
//string destination = _commonFunction.GenerateFolderPath(dhid);
string invalidFiles = "";
var appSettings = ConfigurationManager.AppSettings;
var dropboxFileExt = appSettings["DropboxFileExt"].ToString();
var dropboxFolder = appSettings["DropboxFolder"].ToString();
//var finalDestination = dropboxFolder.Replace("[DEALFOLDER]", destination);
List<string> fileExts = dropboxFileExt.Split(',').ToList<string>();
if (context.Request.Files.Count > 0)
{
foreach (string key in files)
{
HttpPostedFile file = files[key];
string fileName = file.FileName;
string fileType = fileName.Substring(fileName.LastIndexOf('.') + 1);
if(fileExts.Contains(fileType))
{
string dir = System.IO.Path.Combine(@dropboxFolder, fileName);
file.SaveAs(dir);
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
site.js:
$(document).ready(function () {
var box = document.getElementById("rmForm");
box.addEventListener("dragenter", OnDragEnter, false);
box.addEventListener("dragover", OnDragOver, false);
box.addEventListener("drop", OnDrop, false);
var upload = function (files) {
var box = document.getElementById("box");
box.addEventListener("dragenter", OnDragEnter, false);
box.addEventListener("dragover", OnDragOver, false);
box.addEventListener("drop", OnDrop, false);
var data = new FormData();
for (var i = 0; i < files.length; i++) {
data.append(files[i].name, files[i]);
}
$.ajax({
type: "POST",
url: "FileHandler.ashx",
contentType: false,
processData: false,
data: '{\'dhid\':\'' + $('#dhid').val() + '\', \'context\': \'' + data + '\'}',
dataType:"json",
success: function (result) {
alert(result);
},
error: function (xhr) {
alert("There was error uploading files!");
}
});
};
function OnDragEnter(e) {
e.stopPropagation();
e.preventDefault();
}
function OnDragOver(e) {
e.stopPropagation();
e.preventDefault();
}
function OnDrop(e) {
e.stopPropagation();
e.preventDefault();
upload(e.dataTransfer.files);
}
});
好的,我已经解决了
我应该在 url 中传递 dhid 而不是数据。
$.ajax({
type: "POST",
url: "FileHandler.ashx?dhid=" + getUrlParameter('dhid'),
contentType: false,
processData: false,
data: data,
success: function (result) {
alert(result);
},
error: function (xhr) {
alert("There was error uploading files!");
}
});
当我尝试将文件(拖放)传递给 Generic Handler 时,Generic Handler 中的 context.Request.Files.Count 包含 0,我已经尝试了很多方法,但仍然无法解决。
通用处理程序:
CommonFunction _commonFunction = new CommonFunction();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
HttpFileCollection files = context.Request.Files;
string dhidStr = context.Request["dhid"];
int dhid = Convert.ToInt32(dhidStr);
//string destination = _commonFunction.GenerateFolderPath(dhid);
string invalidFiles = "";
var appSettings = ConfigurationManager.AppSettings;
var dropboxFileExt = appSettings["DropboxFileExt"].ToString();
var dropboxFolder = appSettings["DropboxFolder"].ToString();
//var finalDestination = dropboxFolder.Replace("[DEALFOLDER]", destination);
List<string> fileExts = dropboxFileExt.Split(',').ToList<string>();
if (context.Request.Files.Count > 0)
{
foreach (string key in files)
{
HttpPostedFile file = files[key];
string fileName = file.FileName;
string fileType = fileName.Substring(fileName.LastIndexOf('.') + 1);
if(fileExts.Contains(fileType))
{
string dir = System.IO.Path.Combine(@dropboxFolder, fileName);
file.SaveAs(dir);
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
site.js:
$(document).ready(function () {
var box = document.getElementById("rmForm");
box.addEventListener("dragenter", OnDragEnter, false);
box.addEventListener("dragover", OnDragOver, false);
box.addEventListener("drop", OnDrop, false);
var upload = function (files) {
var box = document.getElementById("box");
box.addEventListener("dragenter", OnDragEnter, false);
box.addEventListener("dragover", OnDragOver, false);
box.addEventListener("drop", OnDrop, false);
var data = new FormData();
for (var i = 0; i < files.length; i++) {
data.append(files[i].name, files[i]);
}
$.ajax({
type: "POST",
url: "FileHandler.ashx",
contentType: false,
processData: false,
data: '{\'dhid\':\'' + $('#dhid').val() + '\', \'context\': \'' + data + '\'}',
dataType:"json",
success: function (result) {
alert(result);
},
error: function (xhr) {
alert("There was error uploading files!");
}
});
};
function OnDragEnter(e) {
e.stopPropagation();
e.preventDefault();
}
function OnDragOver(e) {
e.stopPropagation();
e.preventDefault();
}
function OnDrop(e) {
e.stopPropagation();
e.preventDefault();
upload(e.dataTransfer.files);
}
});
好的,我已经解决了
我应该在 url 中传递 dhid 而不是数据。
$.ajax({
type: "POST",
url: "FileHandler.ashx?dhid=" + getUrlParameter('dhid'),
contentType: false,
processData: false,
data: data,
success: function (result) {
alert(result);
},
error: function (xhr) {
alert("There was error uploading files!");
}
});