【求教】C#怎么通过URL传递图片?

Vict0r Chen 2013-09-11 03:54:51
是这样的,我要在CS下发送一个图片,在BS接受然后保存在本地,我一想,就只能通过URL传递啦,请问各位高手,该肿么办,我的思路是这样的,就是先把图片转为字节流通过URL传到BS下,然后Request接受转为图片再保存
...全文
258 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
华为黑名单 2013-09-13
  • 打赏
  • 举报
回复
引用 楼主 u010312979 的回复:
是这样的,我要在CS下发送一个图片,在BS接受然后保存在本地,我一想,就只能通过URL传递啦,请问各位高手,该肿么办,我的思路是这样的,就是先把图片转为字节流通过URL传到BS下,然后Request接受转为图片再保存
将图片生成的字节数存在XML文件中 客户端然后解析
游戏人间 2013-09-12
  • 打赏
  • 举报
回复
刚写了一个比较简单的代码.


        public string Upload(string url, string fileName)
        {
            WebClient webClient = new WebClient();
            byte[] rcv = webClient.UploadFile(url, "POST", fileName);
            return Encoding.UTF8.GetString(rcv);
        }
游戏人间 2013-09-12
  • 打赏
  • 举报
回复
理论上是可行的. 不过当遇到稍大一点的文件时,字符长度就长了. 而一般的浏览对Url长度是有限制的. 所以你的方案行不通的. 以所以最好POST过去就行了.使用WebClient 最简单有个什么Upload就行了.
华为黑名单 2013-09-11
  • 打赏
  • 举报
回复
//客户端处理 XmlDocument doc = new XmlDocument(); doc.Load(Application.StartupPath + @"\update.xml"); XmlElement root = doc.DocumentElement; //取需要下载的文件列表 XmlNode updateNode = root.SelectSingleNode("filelist"); string _Version = root.SelectSingleNode("version").InnerText; string path = updateNode.Attributes["sourcepath"].Value; int count = int.Parse(updateNode.Attributes["count"].Value); for (int i = 0; i < count; i++) { XmlNode itemNode = updateNode.ChildNodes[i]; //从单独的download文件 string fileName = Application.StartupPath + @"\download\" + itemNode.Attributes["name"].Value; string fileRealName = itemNode.Attributes["name"].Value;//取到XML中文件 string[] items = fileRealName.Split(new char[] { '.' }); //对压缩的文件解压缩 单独处理 if (items[items.Length-1] == "zip") { try { string name = Path.GetFileNameWithoutExtension(fileRealName);//去掉 //删除在根目录下的下载文件 fi = new FileInfo(Application.StartupPath + @"\" + name); fi.Delete(); //将下载在download中文件拷到根目录下 File.Copy(fileName, Application.StartupPath + @"\" + name + ".zip"); //解压缩 DecompressFile(Path.GetFileName(fileRealName), name); //解压完成删除压缩包 fi = new FileInfo(Application.StartupPath + @"\" + name + ".zip"); fi.Delete(); continue; } catch { } } if (fileRealName != "update.exe") { try { //删除根路径文件 fi = new FileInfo(Application.StartupPath + @"\" + fileRealName); fi.Delete(); File.Copy(fileName, Application.StartupPath + @"\" + fileRealName); } catch { } }
华为黑名单 2013-09-11
  • 打赏
  • 举报
回复
/// <summary> /// 获取更新软件路径及详细信息(服务端) /// </summary> /// <returns></returns> [WebMethod(Description = "在线更新软件")] public string GetUpdateData() { //取得更新的xml模板内容 XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("update.xml")); XmlElement root = doc.DocumentElement; //看看有几个文件需要更新 XmlNode updateNode = root.SelectSingleNode("filelist"); string path = updateNode.Attributes["sourcepath"].Value; int count = int.Parse(updateNode.Attributes["count"].Value); //将xml中的value用实际内容替换 for (int i = 0; i < count; i++) { XmlNode itemNode = updateNode.ChildNodes[i]; string fileName = path + itemNode.Attributes["name"].Value; FileStream fs = File.OpenRead(Server.MapPath(fileName)); itemNode.Attributes["size"].Value = fs.Length.ToString(); itemNode.Attributes["prefect"].Value = "0"; //为V2.4.3升级准备 BinaryReader br = new BinaryReader(fs); //这里是文件的实际内容,使用了Base64String编码 //itemNode.SelectSingleNode("value").InnerText = Convert.ToBase64String(br.ReadBytes((int)fs.Length), 0, (int)fs.Length); br.Close(); fs.Close(); } return doc.InnerXml; }
华为黑名单 2013-09-11
  • 打赏
  • 举报
回复
你的思路基本上是正确的啊! 我以前做过的一个 客户端自动升级的程序 和你差不多的 就是一个服务器多个客户端程序当 客户端版本低于服务端版本 就更新文件程序代码类似 发你研究下!
lxllb8 2013-09-11
  • 打赏
  • 举报
回复
用post请求吧。

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧