使用Typescript ansyc 编写微信小程序文件上传
2018-03-05 11:41
573 查看
微信小程序的 js 代码是 ES6,当前 TypeScript 的 ansyc 可以编译为 ES3/ES5,因此可以用 ansyc 来编写。
使用 ansyc 来编写,最大的好处是代码可读性,相对于ES5的更容易阅读。另外,TypeScript 强制类型检查,使得代码的出错概率减少。
下面是用 ansyc 和 ES5 的代码对比: // define voice upload function
const uploadVoice = function (next) {
if (!that.data.voice) {
//console.log('no voice to upload');
next(true);
return;
}
const uploadTask = wx.uploadFile({
url: app.globalData.serverOrigin + '/resource?duration=' + that.data.voiceDuration + '&token=' + app.getToken(),
filePath: that.data.voice.tempFilePath,
name: 'resourceFile',
success: function (res) {
//console.log(res);
let resdata = JSON.parse(res.data);
postData.voiceID = resdata.Id;
next(true);
},
fail: function (e) {
console.log('voice upload err:');
console.log(e);
next(false);
}
});
uploadTask.onProgressUpdate(res => {
//console.log('voice file: %d%%', res.progress);
updateProgress(that.data.images.length, res.progress);
});
};
// define after voice uploaded process function
const uploadVoiceNext = function (isSuccess) {
if (isSuccess) {
doPost();
} else {
cancelPost();
}
};
// define after image uploaded process function
const uploadImageNext = function (isSuccess) {
if (isSuccess) {
uploadVoice(uploadVoiceNext);
} else {
cancelPost();
}
};
// define image upload process funciton
const uploadImag = function (i, next) {
if (i == that.data.images.length) {
//console.log('no more image to upload');
next(true);
return;
}
const uploadTask = wx.uploadFile({
url: app.globalData.serverOrigin + '/resource?token=' + app.getToken(),
filePath: that.data.images[i].path,
name: 'resourceFile',
success: function (res) {
//console.log(res);
let resdata = JSON.parse(res.data);
postData.resourceIDs.push(resdata.Id);
if (i < that.data.images.length) {
uploadImag(i + 1, next);
} else {
next(true);
}
},
fail: function (e) {
console.log('image upload err:');
console.log(e);
next(false);
}
});
uploadTask.onProgressUpdate(res => {
//console.log('image file %d: %d%%', i + 1, res.progress);
updateProgress(i, res.progress);
});
};
// begin do upload image one by one and than upload voice by called with uploadimageNext
uploadImag(0, uploadImageNext);
}使用 ansyc 的 ts 代码:function uploadFile(url: string, filePath: string, progress?: (progress: number) => never):Promise<number> {
return new Promise<number>((resolve, reject) => {
const uploadTask = wx.uploadFile({
url: url,
filePath: filePath,
name: 'resourceFile',
success: (res:{data:{Id:number,errMsg?:string},errMsg:string,statusCode:number}) => {
if (res.data.errMsg) throw new Error(`upload file[${filePath}] occurs error:${res.errMsg}`)
resolve(res.data.Id)},
fail: e => reject(e)
});
if (progress){
uploadTask.onProgressUpdate =
(res: { progress: number; totalBytesSent: number; totalBytesExpectedToSend: number; }): never => {
return progress(res.progress)
}
}
});
}
async function post2sever(page: IPage) {
try {
// upload images
let fileIDs:number[] = []
for (let img of page.data.images) {
let id = await uploadFile(Comm.getRequestUrl('/resource'), img)
fileIDs.push(id);
}
// upload voice
let voiceID:number
if (page.data.voice){
voiceID = await uploadFile(
Comm.getRequestUrl(`/resource?duration=${page.data.voice.duration}`), page.data.voice.path)
}
// post
} catch{
}
}注:使用的微信 API d.ts 来自:https://github.com/Emeryao/typed-we-app
编译成 ES5 时,需要设置 tsconfig.json,添加 lib 内容(由于使用了Object.assign 函数,我加的 lib 是 es6,dom,es2015.promise)
参考:https://www.tslang.cn/docs/release-notes/typescript-2.1.html
使用 ansyc 来编写,最大的好处是代码可读性,相对于ES5的更容易阅读。另外,TypeScript 强制类型检查,使得代码的出错概率减少。
下面是用 ansyc 和 ES5 的代码对比: // define voice upload function
const uploadVoice = function (next) {
if (!that.data.voice) {
//console.log('no voice to upload');
next(true);
return;
}
const uploadTask = wx.uploadFile({
url: app.globalData.serverOrigin + '/resource?duration=' + that.data.voiceDuration + '&token=' + app.getToken(),
filePath: that.data.voice.tempFilePath,
name: 'resourceFile',
success: function (res) {
//console.log(res);
let resdata = JSON.parse(res.data);
postData.voiceID = resdata.Id;
next(true);
},
fail: function (e) {
console.log('voice upload err:');
console.log(e);
next(false);
}
});
uploadTask.onProgressUpdate(res => {
//console.log('voice file: %d%%', res.progress);
updateProgress(that.data.images.length, res.progress);
});
};
// define after voice uploaded process function
const uploadVoiceNext = function (isSuccess) {
if (isSuccess) {
doPost();
} else {
cancelPost();
}
};
// define after image uploaded process function
const uploadImageNext = function (isSuccess) {
if (isSuccess) {
uploadVoice(uploadVoiceNext);
} else {
cancelPost();
}
};
// define image upload process funciton
const uploadImag = function (i, next) {
if (i == that.data.images.length) {
//console.log('no more image to upload');
next(true);
return;
}
const uploadTask = wx.uploadFile({
url: app.globalData.serverOrigin + '/resource?token=' + app.getToken(),
filePath: that.data.images[i].path,
name: 'resourceFile',
success: function (res) {
//console.log(res);
let resdata = JSON.parse(res.data);
postData.resourceIDs.push(resdata.Id);
if (i < that.data.images.length) {
uploadImag(i + 1, next);
} else {
next(true);
}
},
fail: function (e) {
console.log('image upload err:');
console.log(e);
next(false);
}
});
uploadTask.onProgressUpdate(res => {
//console.log('image file %d: %d%%', i + 1, res.progress);
updateProgress(i, res.progress);
});
};
// begin do upload image one by one and than upload voice by called with uploadimageNext
uploadImag(0, uploadImageNext);
}使用 ansyc 的 ts 代码:function uploadFile(url: string, filePath: string, progress?: (progress: number) => never):Promise<number> {
return new Promise<number>((resolve, reject) => {
const uploadTask = wx.uploadFile({
url: url,
filePath: filePath,
name: 'resourceFile',
success: (res:{data:{Id:number,errMsg?:string},errMsg:string,statusCode:number}) => {
if (res.data.errMsg) throw new Error(`upload file[${filePath}] occurs error:${res.errMsg}`)
resolve(res.data.Id)},
fail: e => reject(e)
});
if (progress){
uploadTask.onProgressUpdate =
(res: { progress: number; totalBytesSent: number; totalBytesExpectedToSend: number; }): never => {
return progress(res.progress)
}
}
});
}
async function post2sever(page: IPage) {
try {
// upload images
let fileIDs:number[] = []
for (let img of page.data.images) {
let id = await uploadFile(Comm.getRequestUrl('/resource'), img)
fileIDs.push(id);
}
// upload voice
let voiceID:number
if (page.data.voice){
voiceID = await uploadFile(
Comm.getRequestUrl(`/resource?duration=${page.data.voice.duration}`), page.data.voice.path)
}
// post
} catch{
}
}注:使用的微信 API d.ts 来自:https://github.com/Emeryao/typed-we-app
编译成 ES5 时,需要设置 tsconfig.json,添加 lib 内容(由于使用了Object.assign 函数,我加的 lib 是 es6,dom,es2015.promise)
参考:https://www.tslang.cn/docs/release-notes/typescript-2.1.html
低版本异步函数
该特性在TypeScript 2.1之前就已经支持了,但是只能编译为ES6或者ES2015。TypeScript 2.1使其该特性可以在ES3和ES5运行时上使用,这意味着无论您使用什么环境,都可以使用它。注:首先,我们需要确保我们的运行时提供全局的ECMAScript兼容性Promise。这可能需要获取
Promise的polyfill,或者依赖运行时的版本。我们还需要通过设置
lib编译参数,比如
"dom","es2015"或
"dom","es2015.promise","es5"来确保TypeScript知道
Promise可用。
示例
tsconfig.json{ "compilerOptions": { "lib": ["dom", "es2015.promise", "es5"] } }
相关文章推荐
- 夺命雷公狗---微信开发20----编写程序进行上传多媒体文件
- 使用Eclipse 3.1 WST编写Struts文件上传
- 一个使用JAVA编写的类似按键精灵的程序,支持脚本文件编写(含源代码)
- 一个使用JAVA编写的类似按键精灵的程序,支持脚本文件编写(含源代码)
- 5、 java 编写程序拷贝一个文件, 尽量使用效率高的方式.
- 一个使用JAVA编写的类似按键精灵的程序,支持脚本文件编写(含源代码)
- 一个使用JAVA编写的类似按键精灵的程序,支持脚本文件编写(含源代码)
- 怎么让vb编写好的程序生成独立的文件啊,在其他电脑上也能独立使用?
- sourceforge.net专题:空间申请使用、绑定域名、上传文件安装程序
- 习题13:编写程序,使用File类实现删除当前目录下的所有文件。
- 使用APICloud编写优雅的HTML5代码《二》:ajax、文件上传
- 使用input type=file标签进行文件上传时,在安卓手机中的微信浏览器中不能调起相机,但是在苹果手机中的微信浏览器中可以调用相机。解决办法很简单, <input type="file" nam
- 使用Java编写一个简易的本地文件的检索相关字的程序
- 使用java编写程序生成loadrunner参数化文件
- 使用Asp.Net2.0,编写类似GMail的文件上传系统(翻译)
- 一个使用JAVA编写的类似按键精灵的程序,支持脚本文件编写(含源代码)
- [转载红鱼儿]Delphi实现微信开发(3)如何使用multipart/form-data格式上传文件
- 使用Asp.Net2.0,编写类似GMail的文件上传系统
- 编写一个程序,它读取一个文件以判断其中的字节数,然后使用0覆盖所有的字节
- 使用WebClient.UpLoadFile方法在C/S程序中上传文件