Version: 2022.2
Language : English
Creating UnityWebRequests
Creating DownloadHandlers

Creating UploadHandlers

Important: UNet is a deprecated solution, and a new Multiplayer and NetworkingThe Unity system that enables multiplayer gaming across a computer network. More info
See in Glossary
Solution (Netcode for GameObjects) is under development. For more information and next steps see the information on the Unity Netcode for GameObjects website.

Currently, only one type of upload handler is available: UploadHandlerRaw. This class accepts a data buffer at construction time. This buffer is copied internally into native code memory and then used by the UnityWebRequest system when the remote server is ready to accept body data.

Upload Handlers also accept a Content Type string. This string is used for the value of the UnityWebRequest’s Content-Type header if you set no Content-Type header on the UnityWebRequest itself. If you manually set a Content-Type header on the UnityWebRequest object, the Content-Type on the Upload Handler object is ignored.

If you do not set a Content-Type on either the UnityWebRequest or the UploadHandler, the system defaults to setting a Content-Type of application/octet-stream.

UnityWebRequest has a property disposeUploadHandlerOnDispose, which defaults to true. If this property is true, when UnityWebRequest object is disposed, Dispose() will also be called on attached upload handler rendering it useless. If you keep a reference to upload handler longer than the reference to UnityWebRequest, you should set disposeUploadHandlerOnDispose to false.

Example

byte[] payload = new byte[1024];
// ... fill payload with data ...

UnityWebRequest wr = new UnityWebRequest("https://www.mysite.com/data-upload");
UploadHandler uploader = new UploadHandlerRaw(payload);

// Sends header: "Content-Type: custom/content-type";
uploader.contentType = "custom/content-type";

wr.uploadHandler = uploader;
Creating UnityWebRequests
Creating DownloadHandlers
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961