dtjbcda841554 2016-09-23 14:05 采纳率: 0%
浏览 97

将图片从xamarin表单上传到php服务器

Please I have been trying to upload pictures from a xamarin form application to a php server but seems not to be working. The server receives an empty $_FILES request. This is the c# code.

    public async Task<bool> Upload(MediaFile mediaFile, string filename)
    {
        byte[] bitmapData;
        var stream = new MemoryStream();
        mediaFile.GetStream().CopyTo(stream);
        bitmapData = stream.ToArray();
        var fileContent = new ByteArrayContent(bitmapData);

        fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
        fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
        {
            Name = "fileUpload",
            FileName = filename
        };

        string boundary = "---8393774hhy37373773";
        MultipartFormDataContent multipartContent = new MultipartFormDataContent(boundary);
        multipartContent.Add(fileContent);


        HttpClient httpClient = new HttpClient();
        HttpResponseMessage response = await httpClient.PostAsync("http://www.url.com/upload.php", multipartContent);
        response.EnsureSuccessStatusCode();

        if (response.IsSuccessStatusCode)
        {
            string content = await response.Content.ReadAsStringAsync();

            return true;
        }
        return false;
     }

Below is the php file to receive the uploaded image. I tried to save the content of the posted image to file but the file only has an empty array and always return "failure". Please what am i missing wrong? I have searched the web but cant seem to understand the problem.

  $uploads_dir = 'uploads/';
  $req_dump = print_r( $_FILES, true );
  $fp = file_put_contents( 'data.txt', $req_dump );
  if (isset($_FILES["fileUpload"]["tmp_name"]) AND is_uploaded_file($_FILES["fileUpload"]["tmp_name"])) 
  {
    $tmp_name = $_FILES["fileUpload"]["tmp_name"];
    $name = $_FILES["fileUpload"]["name"];
    $Result = move_uploaded_file($tmp_name, "$uploads_dir/$name");
    echo "Success";
  }
  else
  {
    echo "Failure";
  }
  • 写回答

1条回答 默认 最新

  • douyinmian8151 2016-09-23 14:43
    关注

    The AND operator is really not a good choice for you. (On line 4). Sometimes it shows some really unexpected behaviour. (I can refer you to 'AND' vs '&&' as operator for more info).

    If you want a logical AND use the && operator instead. The line would be

    if (isset($_FILES["fileUpload"]["tmp_name"]) && is_uploaded_file($_FILES["fileUpload"]["tmp_name"])) 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)