Comments (12)
var formData=new FormData;
//将文件添加到formdata中,省略...
var p=fly.post(formData)
p.engine.upload.onprogress=function(){
//处理上传进度
}
p.then(()=>{
//成功回调
}).catch(()=>{
//失败回调
})
from fly.
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.
- window.verify是什么东西?
- post第三个参数为啥要传 true
from fly.
window.verify 是一个form表单
<form name="verify" class="inline" id="verify">
<input type="file" name="appFile"/>
<input type="button" value="上传" id="upload"/>
</form>
第三个参数是我胡乱尝试添加的,试图开启异步。实际上是没有作用的,可以忽略。
from fly.
是不是选择的文件太小,而你的网速太快,一下子就传完了
from fly.
不是的,我文件有十几兆,上传了几十秒才传完
from fly.
你可以查一下 xhr.upload.onprogress的相关资料, fly在浏览器中的engine就是xhr
from fly.
我通过以下尝试,发现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.
xhr.open的第三个参数默认就是true, 不用显式传,fly中就是采用的默认值,你去掉第三个参数试试
from fly.
嗯,确如你说说,默认是true,去掉了一样能成功返回。
这样的话,会不会是绑定监听的时机不对呢?
我看到stackoverflow上有这样一个回答
说是监听须在请求开始前绑定才有效,我试了下,把onprogress的绑定放到了send之后,确实是不行的。
from fly.
已修复 用 npm>=0.2.5的包 试试
from fly.
可以了,谢谢
from fly.
Related Issues (20)
- 个别接口如何不走拦截器 HOT 3
- 请求地址后拼参数有问题 HOT 1
- post 请求能同时传递 params 和 body 吗 HOT 1
- 支持拦截fetch请求吗 HOT 1
- 关于请求时TOKEN过期之后怎么再替换最新TOKEN之后再次请求该接口问题 HOT 1
- 小程序 uploadfile上传图片接口好像没走拦截器,导致上传中token过期上传失败没法重新请求? HOT 1
- node端禁止302重定向吗
- 官网上不去了,看不了文档了
- Uncaught ReferenceError: require is not defined HOT 1
- fly + wx.requestSubscribeMessage = fail can only be invoked by user TAP gesture
- 我搞了一个类似的请求库
- 为什么在jest里面始终超时啊
- 支付宝小程序引入 var Fly=require("flyio/dist/npm/ap") put请求在模拟器上可以,在真机上接收不到put的data数据 HOT 2
- 项目不维护更新了么?
- 为什么对 fly 实例解构赋值之后,get 等请求方法都无法使用
- fly请求微信的code2Session对返回的结果取data项返回的是字符串,而不是对象,我们必须手动去转换下 HOT 3
- 支付宝真机
- flyio调用clear时,控制台会报错
- 不同平台的fly实例建议按需到处
- 支付宝小程序引入后,报错
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fly.