2016-06-29 5 views
0

ジェネリックハンドラにファイル(ドラッグ&ドロップ)を渡そうとするとき、ジェネリックハンドラの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); 

} 

})。

答えて

0

さて、私はそれを解決しました。

データの代わりに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!"); 
    } 
}); 
関連する問題