尝试将屏幕快照从Unity上传到Web服务器

时间:2019-03-19 11:15:12

标签: c# php unity3d post xampp

我目前正在尝试上传从Unity截取的屏幕截图并将其发布到我的Web服务器,并将其存储在文件夹中。 (我正在使用 XAMPP 对此进行测试)

我可以毫无问题地截取屏幕截图,但我认为尝试将其作为Post发送到脚本中时做错了。

我试图制作一个基本的 html 表单并对其进行测试,这样我可以成功地存储它,但是在Unity项目中尝试时。什么都没发生。

我有一个名为“ 上载 ”的文件夹,它与 php 脚本位于同一文件夹中。

我一直在寻找答案,但至今未找到任何有效的方法。并且由于所有其他类似的问题都与Unity中的旧版 WWW 功能有关。我决定尝试创建一个新线程。

希望这有一定的道理,我们将不胜感激。

这是我来自 Unity 的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.IO;


public class ScreenShotScript : MonoBehaviour
{
    bool grab = false;
    byte[] bytes;

    private void Update()
    {
        //Press space to start the screen grab
        if (Input.GetKeyDown(KeyCode.Space)) {
            grab = true;
            TakePicture();
        }
    }

    //Function to take a screenshot.
    public void TakePicture()
    {
        if (grab)
        {
            int width = Screen.width;
            int height = Screen.height;

            Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
            tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            tex.Apply();

            bytes = tex.EncodeToPNG();
            Destroy(tex);

            //Testing to see if I can store the image local
            File.WriteAllBytes(Application.dataPath + "/screenShot.png", bytes);
            Debug.Log("Saved the Image!");

            StartCoroutine(SendImage());
        }
    }

    public IEnumerator SendImage()
    {
        List<IMultipartFormSection> formData = new List<IMultipartFormSection>();

        formData.Add(new MultipartFormFileSection("file", bytes));

        UnityWebRequest www = UnityWebRequest.Post("http://localhost/postimage-test.php", formData);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
    }
}

Php脚本:

<!--For testing purpose, to see if I can upload an image by a html form. -->
<html>
    <body>

    <form action="postimage-test.php" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="file">
        <input type="submit" value="Upload Image" name="submit">
    </form>

    </body>
</html>
<?php

  if ($_POST) {

    $target_dir = "upload/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    // Check if image file is a actual image or fake image
    $check = getimagesize($_FILES["file"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }

    //Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    }
    //Else everything is ok, try to upload file
    else {
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    // }

  }
  else {
    echo "No Post! ";
  }
?>

0 个答案:

没有答案