Ionic2和Angular2图片上传示例?

时间:2016-08-07 11:19:56

标签: angularjs angular ionic-framework ionic2

此代码无效..



<ion-item>
    <ion-label stacked>Name</ion-label>
    <ion-input ngControl="name" type="text" placeholder="Enter Category Name"></ion-input>
</ion-item>

<ion-item>
    <ion-label stacked>Image</ion-label>
    <ion-input ngControl="image" type="file" placeholder="Select Category Image"></ion-input>
</ion-item>
&#13;
&#13;
&#13;

有没有办法使用Ion-input上传图像文件,并使用formBuilder在类型脚本中获取上传文件。?

&#13;
&#13;
formBuilder.group({
  name:  ['', Validators.required],
  image: ['', Validators.required]
})
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

要使用Ionic 2 Framework将图像上传到服务器,您必须使用Transfer插件。 使用

安装传输插件
<div id="myNav" class="overlay">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
<div class="overlay-content">
<h>ksadjaskjdaskdjaskdjaskdjaskdjaskdjasdk</h>  </div>
</div>
<button class="abc" style="font-size:30px;cursor:pointer" >&#9776; Accept</button>

 <script type="text/javascript">
 var a="Match";

    $(document).ready(function(){
    $(".abc").click(function(){

        $.ajax({
            type: 'POST',
            url: 'accept.php?w1=<?php echo $id ?>',
            success: function(data) {

               // $("p").text(data);
             if(a=data)
             {
               function openNav()
                {
                  document.getElementById("myNav").style.height = "100%";
                }
             }
             else
             {
               location.reload();         
             }
          }              

        });
      });
     });


  function closeNav() {
  document.getElementById("myNav").style.height = "0%";
   }

然后从Transfer类调用upload函数。

ionic plugin add cordova-plugin-file-transfer
npm install --save @ionic-native/transfer

使用该链接了解更多https://ampersandacademy.com/tutorials/ionic-framework-version-2/upload-an-image-to-the-php-server-using-ionic-2-transfer-and-camera-plugin