使用文件传输Ionic 3上传图像在iOS上不起作用

时间:2019-03-08 17:04:32

标签: asp.net ionic-framework ionic3

在ionic 3中使用文件传输上传图像在Android上效果很好, 但是在模拟器上尝试时在iOS上给我错误.. *这是错误:   the Error

我的离子代码:

chooseImageFromGallery()
{
  this.type="0"

  const options: CameraOptions = {
    quality: 60,
    destinationType: this.camera.DestinationType.FILE_URI,
    encodingType: this.camera.EncodingType.JPEG,
    mediaType: this.camera.MediaType.PICTURE,
    saveToPhotoAlbum:true,
    sourceType:0
  }
  this.camera.getPicture(options)
  .then((imageData) => {
    if (this.platform.is('ios'))
    {
      this.base64Image  = imageData;
    }
    else
    {
      this.base64Image  = imageData;
    }

  this.uploadimage(); // this function to upload img to server

  },
  (err) => {

  }).then((path)=>{

  })

}

uploadimage(){

  this.photoSrc="";

this.translate.get("uploading Image...").subscribe(
  value => {
this.sucesss=false
const fileTransfer: FileTransferObject = this.transfer.create();
let options: FileUploadOptions = {
fileKey: "file",
fileName:'test',
chunkedMode:false,
mimeType:"image/jpeg",
headers:{
Connection:"close"
},
httpMethod: "POST",
  }

  //------------ android ------------//
  this.base64Image =this.base64Image
  //------------ ios ------------//
  //this.base64Image =this.base64Image.substring(28)

  fileTransfer.upload(this.base64Image,encodeURI('mydomain/api/Product/upload'), options)
.then((data:any) => {

    alert("upload success ")


}, (err) => {

  this.translate.get( "error in upload Data").subscribe(
    value => {
      this.service.presentToast(value,2000)
    }
  )
})
  })
}

使用asp.net api2 ..我的服务器代码:

 [HttpPost]
    [Route("upload")]
    [AllowAnonymous]
    public HttpResponseMessage uploadImage()
    {
        var request = HttpContext.Current.Request;
        if (Request.Content.IsMimeMultipartContent())
        {
            foreach (string file in request.Files)
            {
                var postedFile = request.Files[file];
                if (postedFile != null && postedFile.ContentLength > 0)
                {
                    string root = HttpContext.Current.Server.MapPath("~/ServerImg");
                    root = root + "/" + postedFile.FileName;
                    postedFile.SaveAs(root);
                    //Save post to DB
                    return Request.CreateResponse(HttpStatusCode.Found, new
                    {
                        error = false,
                        status = "created",
                        path = root
                    });

                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound, new
                    {
                        error = true


                    });
                }
                // var title = request.Params["title"];


            }

            //  }


            return null;
        }
        else
        {
            return Request.CreateResponse(HttpStatusCode.Forbidden, new
            {
                error = true


            }); 
        }
    }

我花了超过4天的时间..但是对我来说没有任何工作.. 而且此代码在Android上可以正常运行,但在iOS上却不行。 始终上传错误{“ code”:3 ...“ http_status”:500,..

有人可以帮我吗...

0 个答案:

没有答案