有什么方法能ajax上传文件呢 ?



 <form enctype="multipart/form-data" method="post" action="http://A.com/upload.php">
<input type="file" name="file">
<input type="submit">
</form>

正常是这样的吧 , 然后会返回 http://A.com/upload.php , json 数据

怎么实现 在同一页面 ajax 上传呢 ?

jquery post 文件上传 JavaScript angularjs

六氯环己烷 9 years, 8 months ago

可以配合jquery.form 来做 或者用这个 https://github.com/blueimp/jQuery-File-Upload

方得不得了 answered 9 years, 8 months ago

noir87 answered 9 years, 8 months ago

原生js很简单,核心就是FromData对象,下面为实现代码。


 function send(){
    var xhr = new XMLHttpRequest(),
        form = new FormData(),
        metadata = document.getElementById('file').files[0];

    form.append('file',metadata);

    xhr.open("POST", "/test");
    xhr.send(form);
  }

暗暗的布丁 answered 9 years, 8 months ago

好想大聲說你妹 answered 9 years, 8 months ago

Your Answer