开发者社区> 问答> 正文

关于H5使用web直传失败的问题求助

请教个问题,我的app是通过h5混合开发的,在chrome调试的时候可以按照web直传(应用服务器加了token,回调callback)能实现图片上传,但是打包成ios后,通过模拟器进行操作,则上传失败,直接不调取相关的上传回调callback服务,一般会是什么原因,有存在js与ios不兼容的可能么?

展开
收起
song8528 2016-11-01 09:54:33 3329 0
2 条回答
写回答
取消 提交回答
  • 回 1楼shinenuaa的帖子
    你好,代码如下,运行ios模拟器时将 client.multipartUpload上传请求直接跳过,改去执行catch
    var uploadFile = function (client) {
            var file = document.getElementById("publishFile").files[0];
            console.log("---------"+file);
            console.log("token--------------" + token);
            console.log(file.name + ' => ' + key);
            var body = 'token=' + token + '&infoId=' + $rootScope.infoId + '&type=2&imageUrls=http://bucket名字.oss-cn-shanghai.aliyuncs.com/' + key;

            var contentLength = body.length;
            console.log("body ========" + body);
            console.log("bodyLength = " + contentLength);

            return client.multipartUpload(key, file, {
              //progress: progress,
              headers: {
                'x-oss-callback': encodeCallback({
                  url: 'http://服务器地址/oss/callback',
                  //query: {user: 'js-sdk'},
                  //contentType: 'application/json',
                  body: body,
                  contentLength: contentLength
                }),
                'Access-Control-Allow-Origin' : '*'
              }
            }).then(function (res) {
              console.log('upload success: %j', res);
              console.log(res.data);
              $ionicLoading.hide();
              if (res.data.code == 0) {
                UtilsService.alertShowOne("操作成功,前往首页", function () {
                  $state.go("tab.homePage");
                });
              } else {
                UtilsService.alertShowOne("信息发布成功,上传失败图片无法显示",function(){
                  var token = localStorage.getItem("token");
                  var data = {
                    infoId:$rootScope.infoId,
                    token:token
                  };
                  RequestService.request(mConfig.updateInfoImg_url,data).then(function(value){
                    console.log("updateInfoImg_url成功");
                    $state.go("tab.homePage");
                  });
                });
              }
            var uploadFile = function (client) {
            var file = document.getElementById("publishFile").files[0];
            console.log("---------"+file);
            console.log("token--------------" + token);
            console.log(file.name + ' => ' + key);
            var body = 'token=' + token + '&infoId=' + $rootScope.infoId + '&type=2&imageUrls=http://bucket名字.oss-cn-shanghai.aliyuncs.com/' + key;

            var contentLength = body.length;
            console.log("body ========" + body);
            console.log("bodyLength = " + contentLength);
            return client.multipartUpload(key, file, {
              //progress: progress,
              headers: {
                'x-oss-callback': encodeCallback({
                  url: 'http://服务器IP地址/oss/callback',
                  //query: {user: 'js-sdk'},
                  //contentType: 'application/json',
                  body: body,
                  contentLength: contentLength
                }),
                'Access-Control-Allow-Origin' : '*'
              }
            }).then(function (res) {
              console.log('upload success: %j', res);
              console.log(res.data);
              $ionicLoading.hide();
              if (res.data.code == 0) {
                UtilsService.alertShowOne("操作成功,前往首页", function () {
                  $state.go("tab.homePage");
                });
              } else {
                UtilsService.alertShowOne("信息发布成功,上传失败图片无法显示",function(){
                  var token = localStorage.getItem("token");
                  var data = {
                    infoId:$rootScope.infoId,
                    token:token
                  };
                  RequestService.request(mConfig.updateInfoImg_url,data).then(function(value){
                    console.log("updateInfoImg_url成功");
                    $state.go("tab.homePage");
                  });
                });
              };
    2016-11-01 11:03:30
    赞同 展开评论 打赏
  • 出错有什么信息吗? 比如上传失败,oss报了什么错
    2016-11-01 10:48:46
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Web应用系统性能优化 立即下载
高性能Web架构之缓存体系 立即下载
PWA:移动Web的现在与未来 立即下载