简体   繁体   English

在Visual Studio Web表单中使用通用处理程序拖放上传文件

[英]Drag and Drop upload file by using Generic Handler in visual studio webform

when i am trying to pass the files(Drag and drop) to Generic Handler, the context.Request.Files.Count in Generic Handler contains 0, I've tried a lot of ways but still unable to solve it. 当我尝试将文件(拖放)传递给通用处理程序时,通用处理程序中的context.Request.Files.Count包含0,我尝试了很多方法但仍然无法解决。

Generic Handler: 通用处理程序:

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: 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);

}

}); });

Okay, I've solved it. 好的,我已经解决了。

I should pass dhid in url instead of data. 我应该在网址而不是数据中传递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!");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM