Unity 上傳文件

文件地址默認爲保存在 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);
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章