public async static System.Threading.Tasks.Task <string> httpPost(string url, string username, string token, File file) { Bridge.Html5.XMLHttpRequest _http = new XMLHttpRequest(); _http.Open("post", url, true); string returnv = ""; _http.OnReadyStateChange = () => { if (_http.ReadyState == AjaxReadyState.Done) { returnv = _http.ResponseText; } }; FormData formdata = new FormData(); formdata.Append(file.Name, file); formdata.Append("user", username); formdata.Append("token", token); _http.Send(formdata); while (_http.ReadyState != AjaxReadyState.Done) { await System.Threading.Tasks.Task.Delay(100); } return(returnv); }
public static Task <ResultHolder <XMLHttpRequest> > Upload( string interfaceName, string methodName, IEnumerable <Bridge.Html5.File> uploaded, FileUploadOperation operation, RemoteFileId toReplaceOrRemoveId, string jsonizedInput) { //for FormData upload see https://stackoverflow.com/questions/6133800/html5-file-api-readasbinarystring-reads-files-as-much-larger-different-than-fil/6142797#6142797 var uploadData = new FormData(); uploaded.ForEachI((i, x) => uploadData.Append("file" + i, x)); uploadData.Append("operation", ((int)operation).ToString()); uploadData.Append("toReplaceOrRemoveId", JSON.Stringify(toReplaceOrRemoveId)); uploadData.Append("i", jsonizedInput); return(Task.FromPromise <ResultHolder <XMLHttpRequest> >( new XMLHttpRequestImplementingIPromise("POST", $"/{interfaceName}/{methodName}", uploadData), (Func <ResultHolder <XMLHttpRequest>, ResultHolder <XMLHttpRequest> >)(x => x))); }
static void SendFile(ButtonIcon bt, FileInput input) { bt.Disabled = true; var fd = new FormData (); fd.Append ("userfile", input.Files [0]); var rq = fd.Send ("SaveFileUrl"); rq.Done (() => "File Uploaded".LogSuccess (5000)); rq.Fail (()=> "{0}:{1}".Fmt(rq.Status, rq.StatusText).LogError()); rq.Always (() => bt.Disabled=false); }
// sample entry static void Main() { #region setup ocr url and api key var strOcrBaseUri = ConfigurationManager.AppSettings["ocrBaseUri"]; var dicHeader = new Dictionary <string, string> { { "x-api-key", ConfigurationManager.AppSettings["x-api-key"] } }; #endregion #region 1. upload file Console.WriteLine("-----------------------------------------------------------------------"); Console.WriteLine("1. Upload file..."); var formData = new FormData(); formData.Append("method", EnumOcrFileMethod.Upload.ToString()); formData.Append("file", Comm.GetFileData("example.jpg"), "example.jpg"); HttpWebResponse httpWebResponse; RestfulApiBasicResponse restfulApiResponse; try { httpWebResponse = HttpMultiPartRequest.Post(strOcrBaseUri, dicHeader, formData); restfulApiResponse = RestfulApiResponseParser.Parse(httpWebResponse, EnumOcrFileMethod.Upload); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); return; } string strFileName; if (!HandleRestfulApiResponse(restfulApiResponse, EnumOcrFileMethod.Upload, out strFileName)) { return; } #endregion #region 2. recognize the uploaded file Console.WriteLine("{0}-----------------------------------------------------------------------", Environment.NewLine); Console.WriteLine("2. Recognize the uploaded file..."); formData.Clear(); formData.Append("method", EnumOcrFileMethod.Recognize); formData.Append("file_name", strFileName); formData.Append("language", "eng"); formData.Append("output_format", "UFormattedTxt"); formData.Append("page_range", "1-10"); try { httpWebResponse = HttpMultiPartRequest.Post(strOcrBaseUri, dicHeader, formData); restfulApiResponse = RestfulApiResponseParser.Parse(httpWebResponse, EnumOcrFileMethod.Recognize) as RestfulApiRecognizationResponse; } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); return; } if (!HandleRestfulApiResponse(restfulApiResponse, EnumOcrFileMethod.Recognize, out strFileName)) { return; } #endregion #region 3. download the recognized file Console.WriteLine("{0}-----------------------------------------------------------------------", Environment.NewLine); Console.WriteLine("3. Download the recognized file..."); formData.Clear(); formData.Append("method", EnumOcrFileMethod.Download); formData.Append("file_name", strFileName); try { httpWebResponse = HttpMultiPartRequest.Post(strOcrBaseUri, dicHeader, formData); restfulApiResponse = RestfulApiResponseParser.Parse(httpWebResponse, EnumOcrFileMethod.Download) as RestfulApiDownloadResponse; } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); return; } if (!HandleRestfulApiResponse(restfulApiResponse, EnumOcrFileMethod.Download, out strFileName)) { return; } #endregion Console.ReadKey(); }