We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<form id="upload-form" action="upload.php" method="post" enctype="multipart/form-data" > <input type="file" id="upload" name="upload" /> <br /> <input type="submit" value="Upload" /> </form>
html: <form id="upload-form" action="upload.php" method="post" enctype="multipart/form-data" > <input type="file" id="upload" name="upload" /> <br /> <input type="submit" value="Upload" /> </form> js: var form = $("#upload-form"); form.on('submit',function() { var seed = Math.floor(Math.random() * 1000); var id = "uploader-frame-" + seed; var callback = "uploader-cb-" + seed; var iframe = $('<iframe id="'+id+'" name="'+id+'" style="display:none;">'); var url = form.attr('action'); /* 首先,它为表单添加target属性,指向动态插入的iframe窗口,这使得上传结束后,服务器将结果返回iframe窗口,所以当前页面就不会跳转了。其次,它在action属性指定的上传网址的后面,添加了一个参数,使得服务器知道回调函数的名称。这样就能将服务器返回的信息,从iframe窗口传到上层页面。 */ form.attr('target', id).append(iframe).attr('action', url + '?iframe=' + callback); }); window[callback] = function(data){ console.log('received callback:', data); iframe.remove(); //removing iframe form.removeAttr('target'); form.attr('action', url); window[callback] = undefined; //removing callback }; php(upload.php): <script type="text/javascript"> window.top.window['callback'](data); </script>
if (window.FormData) { var formData = new FormData() // 建立一个upload表单项,值为上传的文件 formData.append('upload', document.getElementById('upload').files[0]) var xhr = new XMLHttpRequest() xhr.open('POST', $(this).attr('action')) // 定义上传完成后的回调函数 xhr.onload = function () { if (xhr.status === 200) { console.log('上传成功') } else { console.log('出错了') } } xhr.send(formData) }
css: #holder { border: 10px dashed #ccc; width: 300px; min-height: 300px; margin: 20px auto; } #holder.hover { border: 10px dashed #0c0; } html: <div id="holder"></div> js: // 检查浏览器是否支持拖放上传。 if ('draggable' in document.createElement('span')) { var holder = document.getElementById('holder') holder.ondragover = function () { this.className = 'hover' return false } holder.ondragend = function () { this.className = '' return false } holder.ondrop = function (event) { event.preventDefault() this.className = '' var files = event.dataTransfer.files // do something with files } }
function downloadFile(actoinURL,filePath,fileName){ var form = $("<form>"); $('body').append(form); form.attr('style','display:none'); form.attr('target',''); form.attr('method','post'); form.attr('action',actoinURL);//下载文件的请求路径 var input1 = $('<input>'); input1.attr('type','hidden'); input1.attr('name','filePath'); input1.attr('value',filePath); form.append(input1); var input2 = $('<input>'); input2.attr('type','hidden'); input2.attr('name','fileName'); input2.attr('value',fileName); form.append(input2); form.submit();
html: <a id="download" href="">download</a> js: function downloadFile(fileName, content){ var aLink = document.createElement('a'); var blob = new Blob([content]); aLink.download = fileName; aLink.href = URL.createObjectURL(blob); aLink.click(); } downloadFile('aa.txt', '123')
The text was updated successfully, but these errors were encountered:
up
Sorry, something went wrong.
No branches or pull requests
文件上传
文件下载
The text was updated successfully, but these errors were encountered: