Created
January 30, 2018 04:30
-
-
Save divide-by-zero/d93e60938c0bb686f4ce0637f4cfa346 to your computer and use it in GitHub Desktop.
GyazoSender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
using System.Threading.Tasks; | |
using UnityEngine; | |
using UnityEngine.Networking; | |
public class GyazoSender | |
{ | |
public static IEnumerator UploadImageIterator() | |
{ | |
var filename = Application.temporaryCachePath + "/screenshot.png"; | |
yield return new WaitForEndOfFrame(); | |
ScreenCapture.CaptureScreenshot(filename); | |
yield return new WaitForEndOfFrame(); | |
var byteData = File.ReadAllBytes(filename); | |
var gyazoUploadUrl = "http://upload.gyazo.com/upload.cgi"; | |
var form = new WWWForm(); | |
form.AddBinaryData("imagedata",byteData,"screenshot.png","image/png"); | |
using (var request = UnityWebRequest.Post(gyazoUploadUrl, form)) | |
{ | |
yield return request.SendWebRequest(); | |
var result = request.downloadHandler.text; | |
Debug.Log("result:" + result); | |
Application.OpenURL(result); | |
} | |
// //generate a unique boundary | |
// byte[] boundary = UnityWebRequest.GenerateBoundary(); | |
// //serialize form fields into byte[] => requires a bounday to put in between fields | |
// var requestData = new List<IMultipartFormSection>(){ | |
// new MultipartFormFileSection("imagedata",byteData,"screenshot.png","image/png") | |
// }; | |
// | |
// byte[] formSections = UnityWebRequest.SerializeFormSections(requestData, boundary); | |
// byte[] terminate = Encoding.UTF8.GetBytes(String.Concat("\r\n--", Encoding.UTF8.GetString(boundary), "--")); | |
// | |
// byte[] body = new byte[formSections.Length + terminate.Length]; | |
// Buffer.BlockCopy(formSections, 0, body, 0, formSections.Length); | |
// Buffer.BlockCopy(terminate, 0, body, formSections.Length, terminate.Length); | |
// | |
// string contentType = String.Concat("multipart/form-data; boundary=", Encoding.UTF8.GetString(boundary)); | |
// | |
// using (var www = UnityWebRequest.Post(gyazoUploadUrl, requestData)) | |
// { | |
// www.SetRequestHeader("Content-Type", contentType); | |
// yield return www.SendWebRequest(); | |
// var result = www.downloadHandler.text; | |
// Debug.Log("result:" + result); | |
// } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment