試圖progressMessageHandler從Net.Http.Handlers命名空間使用,但我得到了
命名空間“System.Net.Http”中不存在型別或命名空間名稱“處理程式”(您是否缺少程式集參考?)
我嘗試過的事情:
Visual Studio 的添加參考。不起作用導致統一下載 System.Net.Http dll 并將其放入統一 dll 也不起作用
從 nuget 更新 System.Net.Http
我目前的代碼是:
public static async Task<string> Upload(string uri, string pathFile)
{
byte[] bytes = System.IO.File.ReadAllBytes(pathFile);
using (var content = new ByteArrayContent(bytes))
{
content.Headers.ContentType = new MediaTypeHeaderValue("*/*");
//Send it
var response = await nftClient.PostAsync(uri, content);
response.EnsureSuccessStatusCode();
Stream responseStream = await response.Content.ReadAsStreamAsync();
StreamReader reader = new StreamReader(responseStream);
return reader.ReadToEnd();
}
}
uj5u.com熱心網友回復:
我只會使用UnityWebRequest和使用uploadProgress來更新進度:
UnityWebRequest uwr;
void Start()
{
StartCoroutine(PutRequest("http:///www.yoururl.com"));
StartCoroutine(UploadProgressCoroutine());
}
IEnumerator PutRequest(string url)
{
byte[] dataToPut = System.Text.Encoding.UTF8.GetBytes("Hello, This is a test");
uwr = UnityWebRequest.Put(url, dataToPut);
yield return uwr.SendWebRequest();
if (uwr.isNetworkError)
{
Debug.Log("Error While Sending: " uwr.error);
}
else
{
Debug.Log("Received: " uwr.downloadHandler.text);
}
}
IEnumerator UploadProgressCoroutine()
{
while (!uwr.isDone)
{
HandleProgress(uwr.uploadProgress);
yield return null;
}
}
void HandleProgress(float currentProgress)
{
// currentProgress is value between 0 and 1
}
此處基于Programmer的代碼。
或者對于您的特定用例,您需要創建自己的UnityWebRequest以按照服務器期望的方式獲取標頭和資料:
UnityWebRequest uwr;
void Start()
{
StartCoroutine(PostRequest("http:///www.yoururl.com", "/file/path/here"));
StartCoroutine(UploadProgressCoroutine());
}
IEnumerator PostRequest(string url, string filePath)
{
byte[] dataToPost = System.IO.File.ReadAllBytes(filePath);
uwr = new UnityWebRequest(url, "POST", new DownloadHandlerBuffer(),
new UploadHandlerRaw(dataToPost));
uwr.SetRequestHeader("Content-Type", "*/*");
uwr.SetRequestHeader("Accept", "application/json");
uwr.SetRequestHeader("Authorization", "Bearer " apiToken);
yield return uwr.SendWebRequest();
if (uwr.isNetworkError)
{
Debug.Log("Error While Sending: " uwr.error);
}
else
{
Debug.Log("Received: " uwr.downloadHandler.text);
}
}
IEnumerator UploadProgressCoroutine()
{
while (!uwr.isDone)
{
HandleProgress(uwr.uploadProgress);
yield return null;
}
}
void HandleProgress(float currentProgress)
{
// currentProgress is value between 0 and 1
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/437899.html
下一篇:如何加載png然后轉換為精靈?
