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 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决