【问题标题】:Send image from filesystem to server in AngularJs在AngularJs中将图像从文件系统发送到服务器
【发布时间】:2015-02-14 20:41:22
【问题描述】:

我需要在后台发送多张图片和一些数据到服务器。

我正在为我的 android 应用程序使用 ionicframework(带有 angularjs)。 我正在使用 angular-file-upload (directive),以下代码是我的工厂:

if(angular.isArray(arrPictures) && arrPictures.length > 0) {

       var files = [];
       for(var i=0; i<arrPictures.length; i++) {
           files.push(arrPictures[i].photo); //it is like file://my_path/myImage.jpg
       }

        $upload.upload({
            url: API_POST_ANSWER,
            method: 'POST',
            data: {
                "idSurvey": idSurvey,
                "idOAM": idOAM,
                "idOPM": idOPM,
                "score": score,
                "date": date,
                "answerJson": answersJson,
                "address": address,
                "latitude": latitude,
                "longitude": longitude
            },
            file: files,
            fileFormDataName: "pictures"
        }).progress(function (evt) {
            //progress
            alert('progress: ' + parseInt(100.0 * evt.loaded / evt.total) + '% file :'+ evt.config.file.name);
        }).success(function (data, status, headers, config) {
            // file is uploaded successfully
            alert('file ' + config.file.name + 'is uploaded successfully. Response: ' + data);
        }).error(function (ex) {
            //error
            alert("error");
            alert(ex);
        });

   } 

现在我遇到了一个问题,因为服务器获取的是字符串(路径字符串)而不是文件 我的问题是“如何将路径字符串转换为文件对象以将其正确发送到服务器?”

【问题讨论】:

  • 您使用的是什么服务器?您是否确保服务器实际上可以正确接收文件?

标签: javascript angularjs file-upload ionic-framework


【解决方案1】:

我的解决方案是将图像转换为 base64 并将其存储到本地数据库中(使用 sqllite)。 之后,当我想发送图像时:我查询我的数据库,我得到 base64 字符串并将其发送到我的休息服务。

所有这些解决方案都没有使用 $upload 指令,而是使用 $http 服务。

【讨论】:

  • 您还有其他解决方案吗?
猜你喜欢
  • 2014-01-04
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 2019-11-04
  • 2011-09-09
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
相关资源
最近更新 更多