$(document).on("drop", function(event) { var e = event || window.event; e.preventDefault(); }); $(document).on("dragleave", function(event) { var e = event || window.event; e.preventDefault(); }); $(document).on("dragenter", function(event) { var e = event || window.event; e.preventDefault(); }); $(document).on("dragover", function(event) { var e = event || window.event; e.preventDefault(); });
var box = $(".uploading"); box.on("drop", function(event) { var e = event || window.event; e.preventDefault(); var type = e.originalEvent.dataTransfer.files; if (type.length == 0) { return false; } else if (type.length > 1) { alert("请选择单一文件进行上传"); return false; } else { var resous = window.URL.createObjectURL(type[0]); if (type[0].type.indexOf("image") == 0) { var img = "<img src=" + resous + ">"; box.find("p").remove(); box.find("span").remove(); box.html(img); } else if (type[0].type.indexOf("video") == 0) { var video = '<video controls="controls" src=' + resous + " ></video>"; box.find("p").remove(); box.find("span").remove(); box.html(video); } else { box.html("<p>文件上传</p><span>已选择文件:" + type[0].name + "</span>"); } } resultfile = type[0]; });
$("button").on("click", function() { console.log(resultfile); console.log(1); xhr(); });
function xhr() { var fd = new FormData($("#form1")); fd.append("file", resultfile); $.ajax({ url: "xxx", type: "POST", data: fd, processData: false, contentType: false, success: function(data) { console.log(data); } }); }
|