文件地址默認爲保存在 streamingAssets目錄下的UploadTest.txt文件。
string FilePath = Application.streamingAssetsPath + "/UploadTest.txt";
string url = "XXXXXXXX";
private void Upload()
{
StartCoroutine(IEUpload(url, FilePath, CallBack));
}
private IEnumerator IEUpload(string url,string filePath,Action<string> callBack)
{
UnityWebRequest file = new UnityWebRequest();
WWWForm form = new WWWForm();
file = UnityWebRequest.Get(filePath);
yield return file.SendWebRequest();
form.AddBinaryData("file",file.downloadHandler.data,Path.GetFileName(filePath));
UnityWebRequest req = UnityWebRequest.Post(ConfigData.Instance.serverURL + HttpController.UploadFilePath,form);
yield return req.SendWebRequest();
if (req.isHttpError || req.isNetworkError)
{
Debug.Log(req.error);
}
else
{
Debug.Log("Upload Success");
Debug.Log(req.downloadHandler.text);
}
}
private void CallBack(string str)
{
Debug.Log("Upload Complete : " + str);
}