小程序
小游戏
企业微信
微信支付
扫描小程序码分享
在使用wx.uploadFileAPI的时候,成功之后返回的data为null?这个是什么,求解
正常来讲他这个data中是应该有值的啊
4 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
我这边公众号前端页面上传的图片没有生成预览 同时没有保存到服务器 在后台也不显示,请问大家知道怎样解决吗?
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
你好,怎么做上传图片到七牛
已经知道什么问题了
是因为什么呀?
代码贴出来 分析分析
我这个是想把图片传到七牛,这个是起因。下面的是sdk
// created by gpake
(
function
() {
var
config = {
qiniuRegion:
''
,
qiniuImageURLPrefix:
qiniuUploadToken:
qiniuUploadTokenURL:
qiniuUploadTokenFunction:
null
qiniuShouldUseQiniuFileName:
false
}
module.exports = {
init: init,
upload: upload,
// 在整个程序生命周期中,只需要 init 一次即可
// 如果需要变更参数,再调用 init 即可
init(options) {
};
updateConfigWithOptions(options);
updateConfigWithOptions(options) {
if
(options.region) {
config.qiniuRegion = options.region;
else
{
console.error(
'qiniu uploader need your bucket region'
);
(options.uptoken) {
config.qiniuUploadToken = options.uptoken;
(options.uptokenURL) {
config.qiniuUploadTokenURL = options.uptokenURL;
(options.uptokenFunc) {
config.qiniuUploadTokenFunction = options.uptokenFunc;
(options.domain) {
config.qiniuImageURLPrefix = options.domain;
config.qiniuShouldUseQiniuFileName = options.shouldUseQiniuFileName
upload(filePath, success, fail, options, progress, cancelTask) {
== filePath) {
'qiniu uploader need filePath to upload'
return
;
(options) {
(config.qiniuUploadToken) {
doUpload(filePath, success, fail, options, progress, cancelTask);
(config.qiniuUploadTokenURL) {
getQiniuToken(
});
(config.qiniuUploadTokenFunction) {
config.qiniuUploadToken = config.qiniuUploadTokenFunction();
== config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
'qiniu UploadTokenFunction result is null, please check the return value'
'qiniu uploader need one of [uptoken, uptokenURL, uptokenFunc]'
doUpload(filePath, success, fail, options, progress, cancelTask) {
'qiniu UploadToken is null, please check the init config or networking'
url = uploadURLFromRegionCode(config.qiniuRegion);
fileName = filePath.split(
'//'
)[1];
(options && options.key) {
fileName = options.key;
formData = {
'token'
: config.qiniuUploadToken
(!config.qiniuShouldUseQiniuFileName) {
formData[
'key'
] = fileName
uploadTask = wx.uploadFile({
url: url,
filePath: filePath,
name:
'file'
formData: formData,
success:
(res) {
dataString = res.data
(res.data.hasOwnProperty(
'type'
) && res.data.type ===
'Buffer'
){
dataString = String.fromCharCode.apply(
, res.data.data)
try
dataObject = JSON.parse(dataString);
//do something
imageUrl = config.qiniuImageURLPrefix +
'/'
+ dataObject.key;
dataObject.imageURL = imageUrl;
console.log(dataObject);
(success) {
success(dataObject);
catch
(e) {
console.log(
'parse JSON failed, origin String is: '
+ dataString)
(fail) {
fail(e);
},
fail:
(error) {
console.error(error);
fail(error);
complete:
(res){
console.log(res)
})
uploadTask.onProgressUpdate((res) => {
progress && progress(res)
cancelTask && cancelTask(() => {
uploadTask.abort()
getQiniuToken(callback) {
wx.request({
url: config.qiniuUploadTokenURL,
token = res.data.uptoken;
(token && token.length > 0) {
config.qiniuUploadToken = token;
(callback) {
callback();
'qiniuUploader cannot get your token, please check the uptokenURL or server'
)
'qiniu UploadToken is null, please check the init config or networking: '
+ error);
uploadURLFromRegionCode(code) {
uploadURL =
switch
(code) {
case
'ECN'
: uploadURL =
'https://up.qbox.me'
break
'NCN'
'https://up-z1.qbox.me'
'SCN'
'https://up-z2.qbox.me'
'NA'
'https://up-na0.qbox.me'
'ASG'
'https://up-as0.qbox.me'
default
: console.error(
'please make the region is with one of [ECN, SCN, NCN, NA, ASG]'
uploadURL;
})();
const qiniuUploader = require(
"../../utils/qiniuUploader"
//index.js
// 初始化七牛相关参数
initQiniu() {
options = {
region:
// 华北区
uptokenURL:
shouldUseQiniuFileName:
true
qiniuUploader.init(options);
//获取应用实例
app = getApp()
Page({
data: {
imageObject: {}
//事件处理函数
onLoad:
'onLoad'
that =
this
didPressChooesImage:
didPressChooesImage(that);
didCancelTask:
.data.cancelTask()
didPressChooesImage(that) {
initQiniu();
// 微信 API 选文件
wx.chooseImage({
count: 1,
filePath = res.tempFilePaths[0];
// 交给七牛上传
qiniuUploader.upload(filePath, (res) => {
that.setData({
'imageObject'
: res
}, (error) => {
'error: '
+ JSON.stringify(error));
// , {
// region: 'NCN', // 华北区
// uptokenURL: 'https://[yourserver.com]/api/uptoken',
// domain: 'http://[yourBucketId].bkt.clouddn.com',
// shouldUseQiniuFileName: false
// key: 'testKeyNameLSAKDKASJDHKAS'
// uptokenURL: 'myServer.com/api/uptoken'
// }
// 可以使用上述参数,或者使用 null 作为参数占位符
(progress) => {
'上传进度'
, progress.progress)
'已经上传的数据长度'
, progress.totalBytesSent)
'预期需要上传的数据总长度'
, progress.totalBytesExpectedToSend)
}, cancelTask => that.setData({cancelTask})
这个是测试页面的js,图片能够传到七牛,但是这个API的data没有返回来
写了 有点庞大的 没看懂
自己还是很菜的 不好意思
我好像知道是什么问题了
恭喜 ^_^
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
我这边公众号前端页面上传的图片没有生成预览 同时没有保存到服务器 在后台也不显示,请问大家知道怎样解决吗?
你好,怎么做上传图片到七牛
已经知道什么问题了
是因为什么呀?
代码贴出来 分析分析
我这个是想把图片传到七牛,这个是起因。下面的是sdk
// created by gpake
(
function
() {
var
config = {
qiniuRegion:
''
,
qiniuImageURLPrefix:
''
,
qiniuUploadToken:
''
,
qiniuUploadTokenURL:
''
,
qiniuUploadTokenFunction:
null
,
qiniuShouldUseQiniuFileName:
false
}
module.exports = {
init: init,
upload: upload,
}
// 在整个程序生命周期中,只需要 init 一次即可
// 如果需要变更参数,再调用 init 即可
function
init(options) {
config = {
qiniuRegion:
''
,
qiniuImageURLPrefix:
''
,
qiniuUploadToken:
''
,
qiniuUploadTokenURL:
''
,
qiniuUploadTokenFunction:
null
,
qiniuShouldUseQiniuFileName:
false
};
updateConfigWithOptions(options);
}
function
updateConfigWithOptions(options) {
if
(options.region) {
config.qiniuRegion = options.region;
}
else
{
console.error(
'qiniu uploader need your bucket region'
);
}
if
(options.uptoken) {
config.qiniuUploadToken = options.uptoken;
}
else
if
(options.uptokenURL) {
config.qiniuUploadTokenURL = options.uptokenURL;
}
else
if
(options.uptokenFunc) {
config.qiniuUploadTokenFunction = options.uptokenFunc;
}
if
(options.domain) {
config.qiniuImageURLPrefix = options.domain;
}
config.qiniuShouldUseQiniuFileName = options.shouldUseQiniuFileName
}
function
upload(filePath, success, fail, options, progress, cancelTask) {
if
(
null
== filePath) {
console.error(
'qiniu uploader need filePath to upload'
);
return
;
}
if
(options) {
updateConfigWithOptions(options);
}
if
(config.qiniuUploadToken) {
doUpload(filePath, success, fail, options, progress, cancelTask);
}
else
if
(config.qiniuUploadTokenURL) {
getQiniuToken(
function
() {
doUpload(filePath, success, fail, options, progress, cancelTask);
});
}
else
if
(config.qiniuUploadTokenFunction) {
config.qiniuUploadToken = config.qiniuUploadTokenFunction();
if
(
null
== config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
console.error(
'qiniu UploadTokenFunction result is null, please check the return value'
);
return
}
doUpload(filePath, success, fail, options, progress, cancelTask);
}
else
{
console.error(
'qiniu uploader need one of [uptoken, uptokenURL, uptokenFunc]'
);
return
;
}
}
function
doUpload(filePath, success, fail, options, progress, cancelTask) {
if
(
null
== config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
console.error(
'qiniu UploadToken is null, please check the init config or networking'
);
return
}
var
url = uploadURLFromRegionCode(config.qiniuRegion);
var
fileName = filePath.split(
'//'
)[1];
if
(options && options.key) {
fileName = options.key;
}
var
formData = {
'token'
: config.qiniuUploadToken
};
if
(!config.qiniuShouldUseQiniuFileName) {
formData[
'key'
] = fileName
}
var
uploadTask = wx.uploadFile({
url: url,
filePath: filePath,
name:
'file'
,
formData: formData,
success:
function
(res) {
var
dataString = res.data
if
(res.data.hasOwnProperty(
'type'
) && res.data.type ===
'Buffer'
){
dataString = String.fromCharCode.apply(
null
, res.data.data)
}
try
{
var
dataObject = JSON.parse(dataString);
//do something
var
imageUrl = config.qiniuImageURLPrefix +
'/'
+ dataObject.key;
dataObject.imageURL = imageUrl;
console.log(dataObject);
if
(success) {
success(dataObject);
}
}
catch
(e) {
console.log(
'parse JSON failed, origin String is: '
+ dataString)
if
(fail) {
fail(e);
}
}
},
fail:
function
(error) {
console.error(error);
if
(fail) {
fail(error);
}
},
complete:
function
(res){
console.log(res)
}
})
uploadTask.onProgressUpdate((res) => {
progress && progress(res)
})
cancelTask && cancelTask(() => {
uploadTask.abort()
})
}
function
getQiniuToken(callback) {
wx.request({
url: config.qiniuUploadTokenURL,
success:
function
(res) {
var
token = res.data.uptoken;
if
(token && token.length > 0) {
config.qiniuUploadToken = token;
if
(callback) {
callback();
}
}
else
{
console.error(
'qiniuUploader cannot get your token, please check the uptokenURL or server'
)
}
},
fail:
function
(error) {
console.error(
'qiniu UploadToken is null, please check the init config or networking: '
+ error);
}
})
}
function
uploadURLFromRegionCode(code) {
var
uploadURL =
null
;
switch
(code) {
case
'ECN'
: uploadURL =
'https://up.qbox.me'
;
break
;
case
'NCN'
: uploadURL =
'https://up-z1.qbox.me'
;
break
;
case
'SCN'
: uploadURL =
'https://up-z2.qbox.me'
;
break
;
case
'NA'
: uploadURL =
'https://up-na0.qbox.me'
;
break
;
case
'ASG'
: uploadURL =
'https://up-as0.qbox.me'
;
break
;
default
: console.error(
'please make the region is with one of [ECN, SCN, NCN, NA, ASG]'
);
}
return
uploadURL;
}
})();
const qiniuUploader = require(
"../../utils/qiniuUploader"
);
//index.js
// 初始化七牛相关参数
function
initQiniu() {
var
options = {
region:
'SCN'
,
// 华北区
uptokenURL:
''
,
shouldUseQiniuFileName:
true
};
qiniuUploader.init(options);
}
//获取应用实例
var
app = getApp()
Page({
data: {
imageObject: {}
},
//事件处理函数
onLoad:
function
() {
console.log(
'onLoad'
)
var
that =
this
;
},
didPressChooesImage:
function
() {
var
that =
this
;
didPressChooesImage(that);
},
didCancelTask:
function
() {
this
.data.cancelTask()
}
});
function
didPressChooesImage(that) {
initQiniu();
// 微信 API 选文件
wx.chooseImage({
count: 1,
success:
function
(res) {
var
filePath = res.tempFilePaths[0];
// 交给七牛上传
qiniuUploader.upload(filePath, (res) => {
that.setData({
'imageObject'
: res
});
}, (error) => {
console.error(
'error: '
+ JSON.stringify(error));
},
// , {
// region: 'NCN', // 华北区
// uptokenURL: 'https://[yourserver.com]/api/uptoken',
// domain: 'http://[yourBucketId].bkt.clouddn.com',
// shouldUseQiniuFileName: false
// key: 'testKeyNameLSAKDKASJDHKAS'
// uptokenURL: 'myServer.com/api/uptoken'
// }
null
,
// 可以使用上述参数,或者使用 null 作为参数占位符
(progress) => {
console.log(
'上传进度'
, progress.progress)
console.log(
'已经上传的数据长度'
, progress.totalBytesSent)
console.log(
'预期需要上传的数据总长度'
, progress.totalBytesExpectedToSend)
}, cancelTask => that.setData({cancelTask})
);
}
})
}
这个是测试页面的js,图片能够传到七牛,但是这个API的data没有返回来
写了 有点庞大的 没看懂
自己还是很菜的 不好意思
我好像知道是什么问题了
恭喜 ^_^