private HGImageUploadRes parseHGImageUploadResponse(String json_data) { if (json_data != null) { JObject jo = JObject.Parse(json_data); int status = jo.ContainsKey("status") ? Convert.ToInt32(jo["status"].ToString()) : 0; if (status == 200) { HGImageUploadRes hgiur = JsonNewtonsoft.FromJSON <HGImageUploadRes>(json_data); return(hgiur); } else { throw new Exception(jo.ContainsKey("msg") ? jo["msg"].ToString() : "未知错误:" + json_data); } } return(null); }
public HGImageUploadRes uploadHGFile(String filename) { // TODO: seems fail //String sha256_str = Helpers.EncryptHelper.SHA256(filename, true); //Console.WriteLine("SHA256 for " + filename + " is " + sha256_str); //System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename); //long filelen = fileInfo.Length; //long startpos = 0; //String filemeta = sha256_str + "." + filelen + "." + startpos; //String resturl = "/file/ihongka_files/upload"; //String res = HttpHelper.UploadFileByOffset(FileServerBaseUrl + resturl, filename, 0); //Console.WriteLine(res); // TODO: this seems OK for image but not OK for mp3/mp4 String resturl = "/file/ihongka_files/upload"; // TODO: how to get SHA256 for file // filename = @"E:\work\testmp4.mp4"; String sha256 = EncryptHelper.SHA256(filename, true); // TODO: this is OK FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); String res = Util.HttpRequestPost(FileServerBaseUrl + resturl, "media", filename, fs); // this is also OK without timeout setting // String res = new ApplicationUtil().UploadByHttp(FileServerBaseUrl + resturl, filename); // This doesn't work //filename = @"E:\work\testmp3.mp3"; //String res = WebRequestExtensions.UploadFile(filename, FileServerBaseUrl + resturl, "audio/mp3"); HGImageUploadRes hgiur = parseHGImageUploadResponse(res); return(hgiur); //String resturl = "/file/ihongka_files/upload"; ////MyWebClient wc = new MyWebClient(); ////String res = wc.UploadFile(FileServerBaseUrl + resturl, "file=" + filename); //String res = CurlHelper.PostFile(FileServerBaseUrl + resturl, filename); //HGImageUploadRes hgiur = parseHGImageUploadResponse(res); //return hgiur; }
private void m_pbx_albumcover_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); // dialog.Multiselect = true;//该值确定是否可以选择多个文件 // dialog.Title = "请选择文件夹"; dialog.Filter = "图片文件(*.jpg,*.jpeg,*.png,*.bmp)|*.jpg;*.jpeg;*.png;*.bmp"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string file = dialog.FileName; Console.WriteLine("file is " + file); if (getImageSize(file) > 1) { MessageBox.Show("图片文件不要超过1M"); return; } Image pic = Image.FromFile(file); //strFilePath是该图片的绝对路径 int intWidth = pic.Width; //长度像素值 int intHeight = pic.Height; //高度像素值 Console.WriteLine("file size is " + intWidth + "*" + intHeight); if (intWidth < 300 || intHeight < 300) { MessageBox.Show("图片分辨率不得低于300x300"); return; } m_hg_iur = HGRestfulAPI.getInstance().uploadHGFile(file); if (m_hg_iur == null || m_hg_iur.Status != 200) { MessageBox.Show("图片上传失败"); m_hg_iur = null; } else { m_pbx_albumcover.BorderStyle = BorderStyle.FixedSingle; m_pbx_albumcover.Load(file); } } }