发布(上传文件)
上传文件到服务器也是一个帖子。你可以通过 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 。