private void btn_upload_Click(object sender, EventArgs e) { if (tb_filename.Text.Equals(string.Empty)) { appendLine("文件名不能为空"); return; } try { ITopClient client = new DefaultTopClient(setit.ApiUrl, setit.AppKey, setit.Secret); PictureUploadRequest req = new PictureUploadRequest(); req.PictureCategoryId = 0L; req.Img = new FileItem(tb_filename.Text); req.ImageInputTitle = tb_up_filename.Text; req.Title = tb_up_title.Text; //req.PictureId = 10000L; req.ClientType = "client:computer"; req.IsHttps = true; PictureUploadResponse rsp = client.Execute(req, setit.SessionKey); OnMessage(rsp.Body); Console.WriteLine(rsp.Body); } catch (Exception ex) { appendLine(ex.ToString()); } appendLine(setit.ApiUrl); appendLine(setit.AppKey); appendLine(setit.Secret); appendLine(setit.SessionKey); }
/// <summary> /// 商品图片上传 /// </summary> /// <param name="numIid">商品数字ID</param> /// <param name="filePath">文件路径</param> /// <param name="isMagor">是否将图片设置为主图</param> public string ItemImgUpload(Int64 numIid, string filePath, Boolean isMagor, string sessionKey) { try { ITopClient client = TBManager.GetClient(); PictureUploadRequest req = new PictureUploadRequest(); //req.NumIid = numIid; req.ClientType = "client:phone"; var fileInfo = new FileInfo(filePath); FileItem fItem = new FileItem(fileInfo); req.ImageInputTitle = fileInfo.Name; req.PictureCategoryId = 0; req.Img = fItem; //req.IsMajor = true; //req.Position = 6; PictureUploadResponse response = client.Execute(req, sessionKey); //#if DEBUG // return filePath; //#else return(response.Picture.PicturePath); //#endif } catch (Exception ex) { ExceptionReporter.WriteLog(ex, ExceptionPostion.TopApi); return(null); } }
/// <summary> /// 图片上传 /// </summary> /// <param name="picurl"></param> /// <param name="picname"></param> /// <param name="CategoryId"></param> /// <returns></returns> public string TaobaoUpload(string picurl, string picname, long CategoryId) { Cookie cookie = new Cookie(); string session = cookie.getCookie("top_sessiongroupbuy"); TopXmlRestClient clientaa = new TopXmlRestClient("http://gw.api.taobao.com/router/rest", appkey, secret); PictureUploadRequest request = new PictureUploadRequest(); string filepath = Server.MapPath("images/" + picurl); request.Img = new FileItem(filepath, File.ReadAllBytes(filepath)); request.ImageInputTitle = picurl; request.PictureCategoryId = CategoryId; request.Title = picname; clientaa.PictureUpload(request, session); PictureGetRequest request1 = new PictureGetRequest(); request1.Title = picname; string path = string.Empty; path = clientaa.PictureGet(request1, session).Content[0].PicturePath; return(path); }
public string TaobaoUpload(string picurl, string picname) { string appkey = "12159997"; string secret = "614e40bfdb96e9063031d1a9e56fbed5"; TopXmlRestClient clientaa = new TopXmlRestClient("http://gw.api.taobao.com/router/rest", appkey, secret); PictureUploadRequest request = new PictureUploadRequest(); string filepath = Server.MapPath("images/" + picurl); request.Img = new FileItem(filepath, File.ReadAllBytes(filepath)); request.ImageInputTitle = picurl; request.PictureCategoryId = 0; request.Title = picname; clientaa.PictureUpload(request, session); PictureGetRequest request1 = new PictureGetRequest(); request1.Title = picname; string path = string.Empty; path = clientaa.PictureGet(request1, session).Content[0].PicturePath; return(path); }
public async Task <IActionResult> UploadPicture(PictureUploadRequest request) { var fileFormat = request.File.ContentType; if (request == null || (!fileFormat.Equals("image/jpeg") && !fileFormat.Equals("image/png"))) { return(BadRequest(new { message = "Neteisingas failo formatas" })); } if (_context.ProfilioNuotraukos.Any(e => e.FkProfiliaiid == int.Parse(request.UserId))) { var profilePictoreToRemove = await _context.ProfilioNuotraukos.Where(e => e.FkProfiliaiid == int.Parse(request.UserId)).FirstOrDefaultAsync(); _context.ProfilioNuotraukos.Remove(profilePictoreToRemove); } var pictureFormat = fileFormat.Equals("image/jpeg") ? "jpg" : "png"; var obj = await _pictureService.UploadPictureAsync(request, pictureFormat); var profilePicture = _pictureService.BuildPictureUploadProfilePictureRequest(obj.FilePath, obj.FileSize, pictureFormat, request.UserId); await _context.ProfilioNuotraukos.AddAsync(profilePicture); await _context.SaveChangesAsync(); var picture = await _context.ProfilioNuotraukos.Where((e) => e.FkProfiliaiid == int.Parse(request.UserId)).FirstOrDefaultAsync(); var hostUrl = _httpContextAccessor.HttpContext.Request.Host.Value; hostUrl = "https://" + hostUrl + $"/profilepicturestorage/{request.UserId}.jpg"; return(Ok(new { imagePath = hostUrl })); }
/// <summary> /// 提交事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { string imgs = Request.Form["img1"].ToString(); //string imgs2 = Request.Form["img2"].ToString(); Common.Cookie cookie = new Common.Cookie(); string taobaoNick = cookie.getCookie("nick"); string session = cookie.getCookie("top_sessiongroupbuy"); string sql = string.Empty; Dictionary <string, string> di = new Dictionary <string, string>(); for (int i = 0; i < imgs.Split(',').Length; i++) { sql = "SELECT * FROM TopTaobaoShopImg WHERE nick='" + taobaoNick + "' AND name='" + imgs.Split(',')[i].ToString() + "'"; di.Add(imgs.Split(',')[i].ToString(), ""); FileStream stream = new FileStream("D:\\groupbuy.7fshop.com/wwwroot/top/groupbuy/images/" + imgs.Split(',')[i].ToString(), FileMode.Open); byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, int.Parse(stream.Length.ToString())); stream.Close(); string appkey = "12287381"; string secret = "d3486dac8198ef01000e7bd4504601a4"; TopXmlRestClient client = new TopXmlRestClient("http://gw.api.taobao.com/router/rest", "12287381", "d3486dac8198ef01000e7bd4504601a4"); PictureUploadRequest req = new PictureUploadRequest(); req.PictureCategoryId = 0L; req.ImageInputTitle = imgs.Split(',')[i].ToString(); req.Title = imgs.Split(',')[i].ToString(); req.Img = new Taobao.Top.Api.Util.FileItem(imgs.Split(',')[i].ToString(), bytes); client.PictureUpload(req, session); PictureGetRequest pc = new PictureGetRequest(); pc.Title = imgs.Split(',')[i].ToString(); pc.PictureCategoryId = 0L; Taobao.Top.Api.Domain.PageList <Taobao.Top.Api.Domain.Picture> li = client.PictureGet(pc, session); for (int j = 0; j < li.Content.Count; j++) { sql = "INSERT INTO TopTaobaoShopImg ([nick],[imgSrc] ,[name]) VALUES ('" + taobaoNick + "','" + li.Content[j].PicturePath + "','" + imgs.Split(',')[i].ToString() + "')"; } } }
/// <summary> /// taobao.picture.upload 上传单张图片 /// </summary> /// <param name="req"></param> /// <example> ///example ///ITopClient client = new DefaultTopClient(url, appkey, appsecret); ///PictureUploadRequest req=new PictureUploadRequest(); ///req.PictureCategoryId = 123L; ///FileItem fItem = new FileItem("fileLocation"); ///req.Img = fItem; ///req.ImageInputTitle = "Bule.jpg"; ///req.Title = "图片名称"; ///req.ClientType = "client:computer"; /// PictureUploadResponse response = client.Execute(req, sessionKey); /// </example> /// <returns></returns> public Picture PictureUpload(PictureUploadRequest req) { _log.LogError("正在上传商品描述图片...."); var tContext = InstanceLocator.Current.GetInstance <AuthorizedContext>(); PictureUploadResponse response = _client.Execute(req, tContext.SessionKey); if (response.IsError) { var ex = new TopResponseException(response.ErrCode, response.ErrMsg, response.SubErrCode, response.SubErrMsg, response.TopForbiddenFields); _log.LogError("上传商品描述图片失败", ex); throw ex; } _log.LogError("上传商品描述图片成功"); return(response.Picture); }
public static string uploadLocalImage(string image, string AppKey, string AppSecret, string SessionKey, StreamWriter MovePicLogWriter) { int tryCount = 0; while (true) { if (tryCount < 3) { tryCount++; if (tryCount > 1) { Console.WriteLine("重试上传图片"); MovePicLogWriter.WriteLine("重试上传图片"); } } else { break; } try { ITopClient client = new DefaultTopClient("http://gw.api.taobao.com/router/rest", AppKey, AppSecret); PictureUploadRequest req = new PictureUploadRequest(); req.PictureCategoryId = 0L; FileItem fItem = new FileItem(image); req.Img = fItem; req.ImageInputTitle = Path.GetFileName(image); PictureUploadResponse response = client.Execute(req, SessionKey); if (response != null && response.Picture != null) { Console.WriteLine("上传完成 " + response.Picture.PicturePath); MovePicLogWriter.WriteLine("上传完成 " + response.Picture.PicturePath); return response.Picture.PicturePath; } } catch (System.Net.WebException e) { Console.WriteLine("异常:" + e.Status + " " + e.Message); MovePicLogWriter.WriteLine("异常:" + e.Status + " " + e.Message); } Thread.Sleep(5000); } return ""; }
public static string uploadImage(string image, string AppKey, string AppSecret, string SessionKey, StreamWriter MovePicLogWriter) { Console.WriteLine("上传网络图片:" + image); MovePicLogWriter.WriteLine("上传网络图片:" + image); int tryCount = 0; while (true) { if (tryCount < 3) { tryCount++; if (tryCount > 1) { Console.WriteLine("重试上传图片"); MovePicLogWriter.WriteLine("重试上传图片"); } } else { break; } try { ITopClient client = new DefaultTopClient("http://gw.api.taobao.com/router/rest", AppKey, AppSecret); HttpWebResponse resp = (HttpWebResponse)WebRequest.Create(image).GetResponse(); Console.WriteLine("获取图片返回状态码:" + resp.StatusCode); MovePicLogWriter.WriteLine("获取图片返回状态码:" + resp.StatusCode); if (resp.StatusCode == HttpStatusCode.OK) { Stream sm = resp.GetResponseStream(); MemoryStream ms = new MemoryStream(); byte[] buffer = new byte[4096]; int len; len = sm.Read(buffer, 0, 4096); while (len > 0) { ms.Write(buffer, 0, len); len = sm.Read(buffer, 0, 4096); } byte[] imageBuffer = ms.GetBuffer(); ms.Close(); sm.Close(); PictureUploadRequest req = new PictureUploadRequest(); req.PictureCategoryId = 0L; string fileName = System.DateTime.Now.ToString("yyyyMMddHH:mm:ss"); FileItem fItem = new FileItem(fileName, imageBuffer); req.Img = fItem; req.ImageInputTitle = fileName; PictureUploadResponse response = client.Execute(req, SessionKey); if (response != null && response.Picture != null) { Console.WriteLine("上传完成 " + response.Picture.PicturePath); MovePicLogWriter.WriteLine("上传完成 " + response.Picture.PicturePath); return response.Picture.PicturePath; } } } catch (System.Net.WebException e) { Console.WriteLine("异常:" + e.Status + " " + e.Message); MovePicLogWriter.WriteLine("异常:" + e.Status + " " + e.Message); } Thread.Sleep(5000); } return ""; }
/// <summary> /// 上传单张图片 /// </summary> /// <param name="PicUpload"></param> /// <param name="sessionKey"></param> /// <param name="errorMsg"></param> /// <returns></returns> internal Picture PictureUpload(PictureUpload PicUpload, string sessionKey, out string errorMsg) { errorMsg = null; ITopClient client = new DefaultTopClient(StaticSystemConfig.soft.ApiURL, StaticSystemConfig.soft.AppKey, StaticSystemConfig.soft.AppSecret, "json"); PictureUploadRequest req = new PictureUploadRequest(); if (PicUpload.PictureCategoryId != null) { req.PictureCategoryId = PicUpload.PictureCategoryId; } else { errorMsg = "图片分类ID不能为空!"; return(null); } if (PicUpload.Img != null) { FileItem fItem = new FileItem("ImageName", PicUpload.Img); req.Img = fItem; } else { errorMsg = "上传图片内容不能为空!"; return(null); } if (PicUpload.ImageInputTitle != null) { req.ImageInputTitle = PicUpload.ImageInputTitle; } else { errorMsg = "图片标题不能为空!"; return(null); } if (PicUpload.Title != null) { req.Title = PicUpload.Title; } if (PicUpload.ClientType != null) { req.ClientType = PicUpload.ClientType; } PictureUploadResponse response = client.Execute(req, sessionKey); if (response.IsError) { errorMsg = response.SubErrMsg; return(null); } Top.Api.Domain.Picture pic = response.Picture; Picture newpic = new Picture(); newpic.ClientType = pic.ClientType; newpic.Created = pic.Created; newpic.Deleted = pic.Deleted; newpic.Md5 = pic.Md5; newpic.Modified = pic.Modified; newpic.PictureCategoryId = pic.PictureCategoryId; newpic.PictureId = pic.PictureId; newpic.PicturePath = pic.PicturePath; newpic.Pixel = pic.Pixel; newpic.Referenced = pic.Referenced; newpic.Sizes = pic.Sizes; newpic.Status = pic.Status; newpic.Title = pic.Title; newpic.Uid = pic.Uid; return(newpic); }
/// <summary> /// taobao.picture.upload 上传单张图片 /// </summary> /// <param name="req"></param> /// <example> ///example ///ITopClient client = new DefaultTopClient(url, appkey, appsecret); ///PictureUploadRequest req=new PictureUploadRequest(); ///req.PictureCategoryId = 123L; ///FileItem fItem = new FileItem("fileLocation"); ///req.Img = fItem; ///req.ImageInputTitle = "Bule.jpg"; ///req.Title = "图片名称"; ///req.ClientType = "client:computer"; /// PictureUploadResponse response = client.Execute(req, sessionKey); /// </example> /// <returns></returns> public Picture PictureUpload(PictureUploadRequest req) { _log.LogError("正在上传商品描述图片...."); var tContext = InstanceLocator.Current.GetInstance<AuthorizedContext>(); PictureUploadResponse response = _client.Execute(req, tContext.SessionKey); if (response.IsError) { var ex = new TopResponseException(response.ErrCode, response.ErrMsg, response.SubErrCode, response.SubErrMsg, response.TopForbiddenFields); _log.LogError("上传商品描述图片失败", ex); throw ex; } _log.LogError("上传商品描述图片成功"); return response.Picture; }