[asp.net] 利用WebClient上传图片到远程服务
一、客户端
1.页面
1 <form id="Form1" method="post" runat="server" enctype="multipart/form-data"> <input id="MyFile" type="file" runat="server" /> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="上载文件" OnClick="Button1_Click"></asp:Button> </form>
2.后台
System.Web.HttpFileCollection oFiles = System.Web.HttpContext.Current.Request.Files;
].FileName;
);
].ContentLength];
System.IO.Stream fs = (System.IO.Stream)oFiles[].InputStream;
fs.Read(b, , oFiles[].ContentLength);
string postData = "data=" + HttpUtility.UrlEncode(Convert.ToBase64String(b));
var webclient = new WebClient();
webclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
//byte[] buffer = webclient.UploadData("http://localhost/datapush/DataPush.ashx", "POST", byteArray);//ashx
byte[] buffer = webclient.UploadData("http://localhost/datapush/WebServiceDataPush.asmx/DataPush", "POST", byteArray);//asmx
var msg = Encoding.UTF8.GetString(buffer);
Response.Write(msg);
二、服务端
string msg = "";
byte[] filedata = Convert.FromBase64String(context.Request["data"]);
)
{
msg= "{\"code\":\"上传的是空文件\"}";
}
)
{
msg = "{\"code\":\"图片大小不能超过1M\"}";
}
].ToString() + filedata[].ToString();
")
{
fileextension = "gif";
}
")
{
fileextension = "jpg";
}
")
{
fileextension = "png";
}
")
{
fileextension = "bmp";
}
")
{
fileextension = "tif";
}
else
{
msg = "{\"code\":\"上传的文件不是图片\"}";
}
try
{
//保存图片
string filename = Guid.NewGuid().ToString("D") + "." + fileextension;
System.IO.MemoryStream ms = new System.IO.MemoryStream(filedata);
System.IO.FileStream fs = new System.IO.FileStream(context.Server.MapPath("~/") + "/采集图片/" + filename, System.IO.FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();
fs = null;
ms = null;
msg = "{\"code\":\"上传图片成功\"}";
}
catch (Exception exe)
{
msg = "{\"code\":\"" + exe.Message + "\"}";
}
[asp.net] 利用WebClient上传图片到远程服务的更多相关文章
- c#利用WebClient和WebRequest获取网页源代码的比较
前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...
- SpringCloud03 Ribbon知识点、 Feign知识点、利用RestTemplate+Ribbon调用远程服务提供的资源、利用feign调用远程服务提供的资源、熔断
1 远程服务资源的调用 1.1 古老的套路 在微服务出现之前,所有的远程服务资源必须通过RestTemplate或者HttpClient进行:但是这两者仅仅实现了远程服务资源的调用,并未提供负载均衡实 ...
- winform客户端利用webClient实现与Web服务端的数据传输
由于项目需要,最近研究了下WebClient的数据传输.关于WebClient介绍网上有很多详细介绍,大概就是利用WebClient可以实现对Internet资源的访问.无外乎客户端发送请求,服务端处 ...
- c#利用WebClient和WebRequest获取网页源代码
C#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取网页源代码 WebClient类 WebClient ...
- [ASP.NET]利用itextsharp将GridView汇出PDF档
原文 [ASP.NET]利用itextsharp将GridView汇出PDF档 最近在讨论区看到有人说itextsharp可以把网页变成PDF 小弟就去抓一下itextsharp来玩玩,先教大家最实用 ...
- 利用WebClient上传参数及文件流到远程ashx服务
原文 利用WebClient上传参数及文件流到远程ashx服务 1 思路: WebClient.UploadFile()方法可以上传文件:UploadData()方法可以上传数据参数:如何合二为一既上 ...
- asp.net利用存储过程分页代码
-最通用的分页存储过程 -- 获取指定页的数据 CREATE PROCEDURE Pagination ), -- 表名 ) = '*', -- 需要返回的列 )='', -- 排序的字段名 , -- ...
- 看代码网备份|利用WebClient|eKing.CmdDownLoadDbBakOper|实现定时拷贝数据库备份文件到文件服务器
摘要: 1.有两台服务器 (1)看代码网(记为A):内网IP:10.186.73.30 (2)文件服务器(记为B):内网IP:10.135.87.157 2.在A架设一个网站,端口8088(防火强设置 ...
- 一个简单的利用 WebClient 异步下载的示例(三)
继续上一篇 一个简单的利用 WebClient 异步下载的示例(二) 后,继续优化它. 1. 直接贴代码了: DownloadEntry: public class DownloadEntry { p ...
随机推荐
- SQLServer中处理每天四亿三千万记录
我是如何在SQLServer中处理每天四亿三千万记录的 首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文中有不对的地 ...
- openwrt补丁
http://wiki.openwrt.org/doc/devel/patches 中文文档:http://andelf.diandian.com/post/2013-05-22/4005067737 ...
- leetcode 第42题 Multiply Strings
题目:Given two numbers represented as strings, return multiplication of the numbers as a string. Note: ...
- leetcode第16题--3Sum Closest
Problem:Given an array S of n integers, find three integers in S such that the sum is closest to a g ...
- [置顶] Android开发之Thread类分析
在我们Linux系统中创建线程函数为:pthread_create(),在Android中我们为线程封装了一个类Thread,实际调用的还是pthread_create() 当我们想创建线程的时候,只 ...
- 【转】【Android工具】被忽略的UI检视利器:Hierarchy Viewer
原文:http://blog.csdn.net/ddna/article/details/5527072 Hierarchy Viewer是随AndroidSDK发布的工具,位置在tools文件夹下, ...
- 启用密码管理之前创建的用户连接Oracle报ORA-28002处理一则
处理方法其实很简单.只要: alter user <username> identified by <same password>; 这个操作后,恢复正常了 下面作个 ...
- validate大表单验证
Vaidate 插件 在前端开发中, 我们会遇到大表单的验证和组合成JSON, 这是一项巨大的任务, 如果都通过 手动编写低级代码来实现 50+ input类型的验证和复杂JSON的组装, 这无疑是异 ...
- HAProxy+apache实现web服务动静分离
HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支 ...
- GLFW3出error adding symbols: DSO missing from command line解决
背景:使用OpenGL的GLFW3.1库的时候,使用其中一些代码 报error adding symbols: DSO missing from command line 因为使用的是Qcreator ...