简体   繁体   中英

Upload a wav file to server in Unity3d C#

I need to upload wav files to a server in c# in unity3d.

I made already a upload system with png files but for some reason it doesn't work with wav files

Here is the image upload script I changed it so it will work with audio but it doesn't work the error I still get is from the php file which says failed

using UnityEngine;
using System.Xml;
using System.IO;
using System.Collections;
using System.Text;


public class Upload : MonoBehaviour
{



// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnGUI()
{
    GUI.Label(new Rect(100, 0, 500, 20), Application.dataPath);
    if (GUI.Button(new Rect(100, 100, 150, 20), "Upload"))
    {
        UploadFile("http://localhost:8888/unity/upload.php");
    }
}

IEnumerator UploadFileCo(string uploadURL)
{
    WWW localFile = new WWW("file:///1.wav");
    yield return localFile;
    WWWForm postForm = new WWWForm();
    postForm.AddBinaryData("file", localFile.bytes, "1.wav");
    WWW upload = new WWW(uploadURL, postForm);
    yield return upload;
    if (upload.error == null)
    {
        Debug.Log(upload.text);
        Debug.Log ("upload error null");
    }
    else
    {
        Debug.Log("Error during upload: " + upload.error);
    }
}

void UploadFile(string uploadURL)
{
    StartCoroutine(UploadFileCo(uploadURL));
}

}

Here is the php script

<?php
$thefile = $_FILES['file'];

 if(!empty($theFile))
 {
 echo "Success!";
 }
 else
 {
 echo "Failed!";
 }
 ?>

there is a huge mistake in this PHP script.

it reads:

if(!empty($theFile))

should be:

if(!empty($thefile))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM