Javascript 如何将图像发送到AngularJS上的服务器?

Javascript 如何将图像发送到AngularJS上的服务器?,javascript,angularjs,Javascript,Angularjs,我正在尝试将带有一些附加数据的图像发送到服务器。我的问题是图像没有保存在其他录制的服务器上 下面是我的.html代码: <div class="widget uib_w_4 d-margins" data-uib="media/file_input" data-ver="0"> <input type="file" name="image" id="image" accept="image/jpeg, image/png, image/gif"

我正在尝试将带有一些附加数据的图像发送到服务器。我的问题是图像没有保存在其他录制的服务器上

下面是我的.html代码:

<div class="widget uib_w_4 d-margins" data-uib="media/file_input" data-ver="0">
   <input type="file" name="image" id="image" 
      accept="image/jpeg, image/png, image/gif" 
      ng-model="image" class="ng-pristine ng-untouched ng-valid">
</div>
<div class="widget uib_w_5 d-margins" data-uib="media/text" data-ver="0">
   <div class="widget-container left-receptacle"></div>
   <div class="widget-container right-receptacle"></div>
   <div>
      <p>Username:</p>
   </div>
</div>

用户名:

JS代码

< script >
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $scope.SendData = function()
    var data = $.param({
        image: $scope.image,
        username: $scope.username,
    });

    var config = {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded;
                charset = utf - 8;
                '
        }
    }
    var param = getParameterByName('id');
    $http.post('myrurl.php', data, config)
        .success(function(data, status, headers, config) {
        window.location.href = 'view.html';
    })
        .error(function(data, status, header, config) {
        alert('Please retry');
    });
};
}); 
< /script>

我尝试使用print\u r($\u请求)在PHP中打印值

image的值为null


如何将图像发送到服务器?

非常简单。一个好的方法是对图像进行Base64编码,然后将Base64发送到服务器;最好的部分是PHP本机支持对Base64进行解码

您可以执行以下操作:

<!-- In your HTML file -->
<input type="file" id="file" /> 
<input type="submit" ng-click"uploadFile" value="Upload File"/>
最后,在PHP文件中,您可以:

$imageData = base64_decode( $_GET[ 'data' ] ); // Now you have a binary image file. Do whatever you want!

你如何处理服务器端的事情?服务器代码很好,我试图通过打印请求检查图像值是否为空,我在服务器上使用单独的enpoint仅用于图像上载。用户必须先上传图像,然后发送表单数据。。但是,无论哪种方式,我在将图像发送到服务器之前都会将其作为formData进行准备。你可以创建fiddle示例或引导我示例url,但我不想作为两个请求发送检查第三个答案你能告诉我有关错误的更多信息吗?另外,请记住,此代码只是一个示例。你必须根据你的需要编辑它。在我的机器上工作得很好!等待你试过调试它吗?是否包含jQuery?这是完整的代码,需要根据需要进行编辑。我只是给你一些信息,这些信息是让球滚动所必需的。@weirdpanda
$imageData = base64_decode( $_GET[ 'data' ] ); // Now you have a binary image file. Do whatever you want!