釋出(上傳檔案)

上傳檔案到伺服器也是一個帖子。你可以通過 WWW 輕鬆上傳檔案,如下所示:

將 Zip 檔案上載到伺服器

string mainUrl = "http://server/upload/";
string saveLocation;

void Start() 
{
    saveLocation = "ftp:///home/xxx/x.zip"; // The file path.
    StartCoroutine(PrepareFile());
}

// Prepare The File.
IEnumerator PrepareFile() 
{
    Debug.Log("saveLoacation = " + saveLocation);
     
    // Read the zip file.
    WWW loadTheZip = new WWW(saveLocation);

    yield return loadTheZip;

    PrepareStepTwo(loadTheZip);
}

void PrepareStepTwo(WWW post) 
{
    StartCoroutine(UploadTheZip(post));
}

// Upload.
IEnumerator UploadTheZip(WWW post) 
{
    // Create a form.
    WWWForm form = new WWWForm();
 
    // Add the file.
    form.AddBinaryData("myTestFile.zip",post.bytes,"myFile.zip","application/zip");
 
    // Send POST request.
    string url = mainUrl;
    WWW POSTZIP = new WWW(url,form);
 
    Debug.Log("Sending zip...");
    yield return POSTZIP;
    Debug.Log("Zip sent!");
}

在這個例子中,它使用協程來準備和上傳檔案,如果你想了解更多關於 Unity 協同程式的資訊,請訪問 Coroutines