Giter Site home page Giter Site logo

Comments (12)

wendux avatar wendux commented on August 11, 2024 1
var formData=new FormData;
//将文件添加到formdata中,省略...

var p=fly.post(formData)
p.engine.upload.onprogress=function(){
 //处理上传进度
}
p.then(()=>{
//成功回调
}).catch(()=>{
//失败回调
})

from fly.

toutouli avatar toutouli commented on August 11, 2024
         document.getElementById('upload').onclick=function(){
            var formData = new FormData(window.verify);
            var p=fly.post('<%= api %>/channel/upload/',formData,true)
            p.engine.upload.onprogress=function(e){
                console.log(e)
            }
            p.then(function(data){
                console.log(data)
            })
        }

我按照你说的那样尝试了一下,直到上传成功,onprogress才调用了一次,是我写得不对吗?还是需要配置别的参数?

from fly.

wendux avatar wendux commented on August 11, 2024
  1. window.verify是什么东西?
  2. post第三个参数为啥要传 true

from fly.

toutouli avatar toutouli commented on August 11, 2024

window.verify 是一个form表单

<form name="verify" class="inline" id="verify">
     <input type="file" name="appFile"/>
     <input type="button" value="上传" id="upload"/>
</form>

第三个参数是我胡乱尝试添加的,试图开启异步。实际上是没有作用的,可以忽略。

from fly.

wendux avatar wendux commented on August 11, 2024

是不是选择的文件太小,而你的网速太快,一下子就传完了

from fly.

toutouli avatar toutouli commented on August 11, 2024

不是的,我文件有十几兆,上传了几十秒才传完

from fly.

wendux avatar wendux commented on August 11, 2024

你可以查一下 xhr.upload.onprogress的相关资料, fly在浏览器中的engine就是xhr

from fly.

toutouli avatar toutouli commented on August 11, 2024

我通过以下尝试,发现xhr开启异步请求是能够成功返回进度(即xhr.open的第三个参数为true时)
那么如何通过fly开启异步?

document.getElementById('upload').onclick=function(){
            var formData = new FormData(window.verify);
            xhr=new XMLHttpRequest();
            xhr.onreadystatechange=function(){
                console.log(xhr.response)
            }
            xhr.upload.onprogress=function(e){
                console.log(e)
            }
            xhr.open('POST','<%= background %>/channel/upload/',true);
            xhr.send(formData)
 }

from fly.

wendux avatar wendux commented on August 11, 2024

xhr.open的第三个参数默认就是true, 不用显式传,fly中就是采用的默认值,你去掉第三个参数试试

from fly.

toutouli avatar toutouli commented on August 11, 2024

嗯,确如你说说,默认是true,去掉了一样能成功返回。
这样的话,会不会是绑定监听的时机不对呢?
我看到stackoverflow上有这样一个回答
说是监听须在请求开始前绑定才有效,我试了下,把onprogress的绑定放到了send之后,确实是不行的。

from fly.

wendux avatar wendux commented on August 11, 2024

已修复 用 npm>=0.2.5的包 试试

from fly.

toutouli avatar toutouli commented on August 11, 2024

可以了,谢谢

from fly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.