/// <summary> /// Post image data on server /// </summary> /// <param name="image">Image data</param> /// <param name="type">Image extension type</param> /// <param name="param">Recognize params</param> public void PostImage(byte[] image, ImgType type, RecognizeParams param = null) { if (image == null || image.Length == 0) { throw new ArgumentException("Bad image data"); } List <KeyValuePair <string, string> > postParams = new List <KeyValuePair <string, string> >(); postParams.Add(new KeyValuePair <string, string>("method", "post")); postParams.Add(new KeyValuePair <string, string>("key", ACCOUNT_ID)); if (param != null) { foreach (var keyValue in param.Params) { postParams.Add(keyValue); } } byte[] fbData = BuldFormbasedData(postParams, StreamEncoding.GetString(image), type); string contentType = "multipart/form-data; boundary=" + _boundary; RequestParams reqParams = new RequestParams(null, null, contentType, null, true, "POST", StreamEncoding, false); DownloaderObj obj = new DownloaderObj(_postUri, EndPostImage, true, null, CookieOptions.Empty, 4, null, null, false, 1000, null, reqParams); obj.PostData = fbData; Downloader.Queue(obj); }
private string BuldFileFormStr(string boundary, ImgType imgType, string value) { string fs = string.Empty; fs += "--" + boundary + "\r\n"; fs += "Content-Disposition: form-data; name=\"file\"; filename=\"image.jpeg\"\r\n"; fs += "Content-Type: " + imgType + "\r\n\r\n"; fs += value + "\r\n"; return(fs); }
/// <summary> /// Build formbased post data source /// </summary> /// <param name="keyValueParams">Recognize params</param> /// <param name="image">Image data as string</param> /// <param name="imgType">Image extension type</param> /// <returns>Formbase data buffer</returns> private byte[] BuldFormbasedData(List <KeyValuePair <string, string> > keyValueParams, string image, ImgType imgType) { StringBuilder formatBasedStr = new StringBuilder(); foreach (var pair in keyValueParams) { formatBasedStr.Append(BuldParamFormStr(_boundary, pair.Key, pair.Value)); } formatBasedStr.Append(BuldFileFormStr(_boundary, imgType, image)); formatBasedStr.AppendFormat("{0}{1}{0}", "--", _boundary); string final = formatBasedStr.ToString(); return(StreamEncoding.GetBytes(formatBasedStr.ToString())); }
private string BuldFileFormStr(string boundary, ImgType imgType, string value) { string fs = string.Empty; fs += "--" + boundary + "\r\n"; fs += "Content-Disposition: form-data; name=\"file\"; filename=\"image.jpeg\"\r\n"; fs += "Content-Type: " + imgType + "\r\n\r\n"; fs += value + "\r\n"; return fs; }
/// <summary> /// Post image data on server /// </summary> /// <param name="image">Image data</param> /// <param name="type">Image extension type</param> /// <param name="param">Recognize params</param> public void PostImage(byte[] image, ImgType type, RecognizeParams param = null) { if (image == null || image.Length == 0) throw new ArgumentException("Bad image data"); List<KeyValuePair<string, string>> postParams = new List<KeyValuePair<string, string>>(); postParams.Add(new KeyValuePair<string, string>("method", "post")); postParams.Add(new KeyValuePair<string, string>("key", ACCOUNT_ID)); if (param!=null) { foreach (var keyValue in param.Params) { postParams.Add(keyValue); } } byte[] fbData = BuldFormbasedData(postParams, StreamEncoding.GetString(image), type); string contentType = "multipart/form-data; boundary=" + _boundary; RequestParams reqParams = new RequestParams(null, null, contentType, null, true, "POST", StreamEncoding, false); DownloaderObj obj = new DownloaderObj(_postUri, EndPostImage, true, null, CookieOptions.Empty, 4, null, null, false, 1000, null, reqParams); obj.PostData = fbData; Downloader.Queue(obj); }
/// <summary> /// Build formbased post data source /// </summary> /// <param name="keyValueParams">Recognize params</param> /// <param name="image">Image data as string</param> /// <param name="imgType">Image extension type</param> /// <returns>Formbase data buffer</returns> private byte[] BuldFormbasedData(List<KeyValuePair<string, string>> keyValueParams, string image, ImgType imgType) { StringBuilder formatBasedStr = new StringBuilder(); foreach (var pair in keyValueParams) { formatBasedStr.Append(BuldParamFormStr(_boundary, pair.Key, pair.Value)); } formatBasedStr.Append(BuldFileFormStr(_boundary, imgType, image)); formatBasedStr.AppendFormat("{0}{1}{0}", "--", _boundary); string final = formatBasedStr.ToString(); return StreamEncoding.GetBytes(formatBasedStr.ToString()); }