webService 

  /// <summary>
/// 上传图片webServer 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public bool UpdateFile(byte[] content, string pathand,string filename)
{
string pathandname = pathand + filename;
int index = pathandname.LastIndexOf(".");
if (index == )
{
return false;
}
else
{
string extended = string.Empty;
if (index + == pathandname.Length)
{
return false;
}
else
{
extended = pathandname.Substring(index + );
if (extended == "jpeg" || extended == "gif" || extended == "jpg" ||
extended == "bmp" || extended == "png")
{
try
{
if (!Directory.Exists(@pathand))//若文件夹不存在则新建文件夹
{
Directory.CreateDirectory(@pathand); //新建文件夹
} //File.WriteAllBytes(Server.MapPath(pathandname), content);
File.WriteAllBytes(pathandname, content);
return true;
}
catch (Exception ex)
{
return false;
}
}
else
{
return false;
}
}
}
}
} //测试 private void btnSaveServer_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string pathand = CommonClass.Config.GetAppSettings<string>("ProductImageUrl",
@"D:\FSTERP\ProductImage\");
string imagename = "mylove";
bool uploadResult = UploadImageWebService(fileDialog.FileName, pathand, imagename);
if (uploadResult)
MessageBox.Show("上传成功!");
else
MessageBox.Show("上传失败!");
}
}
/// <summary>
/// 上传图片[通过webServer]
/// </summary>
/// <param name="filename">选择图片路径[默认选择文件包括后缀名]</param>
/// <param name="pathand">上传服务器文件夹[文件夹不存在则新建]</param>
/// <param name="imagename">上传后图片文件名[不包括后缀名]</param>
/// <returns>上传结果</returns>
public bool UploadImageWebService(string filename, string pathand, string imgname)
{ string extension = Path.GetExtension(filename).ToLower().Replace(".", "");
string paramSuffix = "|" + CommonClass.Config.GetAppSettings<string>("ImageFormat",
"jpg|jpge|gif|bmp|png") + "|";
int pi = paramSuffix.IndexOf("|" + extension + "|");
if (pi < )
{
MessageBox.Show("仅能上传jpg|jpge|gif|bmp|png格式的图片!");
return false;
}
else
{
FileInfo fileInfo = new FileInfo(filename);
if (fileInfo.Length > )
{
MessageBox.Show("上传的图片不能大于20K");
}
else
{
//Stream file = fileDialog.OpenFile();
FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read);
byte[] bytes = new byte[file.Length];
file.Read(bytes, , bytes.Length);
//实例化WebService服务。ServiceReference1是我们在添加引用时设置的命名空间
WebService.WebService1 webservice = new FSTERP.WebService.WebService1();
DateTime time = DateTime.Now;
//重命名图片的名称与路径
//string pathand = CommonClass.Config.GetAppSettings<string>("ProductImageUrl",
@"D:\FSTERP\ProductImage\");
string imagename = imgname + "." + extension;
//string pathandname = pathand + imagename;
if (webservice.UpdateFile(bytes, pathand, imagename))
{
return true;
}
else
{
return false;
}
}
}
return false;
}

测试图片

版权声明:本文为博主原创文章,未经博主允许不得转载。

webService上传图片的更多相关文章

  1. Sql server之路 (六)上传服务器图片

    原理: 上传图片的名字 插入到数据库里 上传图片的内容(二进制数据) 写到服务器指定的目录下 下次读取图片的时候 从数据库里的指定字段里读取图片文件名 从数据库的指定路径下 拼串成完成的路径 就可以下 ...

  2. webservice跨域上传图片

    1.上传文件,在一般处理程序中处理 //1.接收post过来的文件 HttpPostedFile file = context.Request.Files[]; || file.ContentLeng ...

  3. android之调用webservice 实现图片上传

    转:http://www.cnblogs.com/top5/archive/2012/02/16/2354517.html public void testUpload(){ try{ String ...

  4. WebService上传下载图片

    WebService服务端 接受要上传的图片 public string UploadImg(byte[] fileBytes, int id) { try { string filePath = M ...

  5. Android使用ksoap2调用C#中的webservice实现图像上传

    目录: 一. android使用ksoap2调用webservice 二. 异步调用 三. Android使用ksoap2调用C#中的webservice实现图像上传参考方法 四. 图像传输中Base ...

  6. iOS开发之结合asp.net webservice实现文件上传下载

    iOS开发中会经常用到文件上传下载的功能,这篇文件将介绍一下使用asp.net webservice实现文件上传下载. 首先,让我们看下文件下载. 这里我们下载cnblogs上的一个zip文件.使用N ...

  7. 博客使用BOS上传图片

    1.博客平台的选定 从大学开始做个人主页算起,最开始是使用html,CSSS写简单的页面,后面大学毕业之后接触到了WordPress,就开始用WordPress搭建网站.现在还维护着一个农村网站.ht ...

  8. nodejs利用ajax实现网页无刷新上传图片

    nodejs利用ajax实现网页无刷新上传图片 标签(空格分隔): nodejs 通常情况下上传图片是要通过提交form表单来实现的,但是这又不可避免的产生了网页转. 利用ajax技术和FormDat ...

  9. webService

    什么是webService WebService,顾名思义就是基于Web的服务.它使用Web(HTTP)方式,接收和响应外部系统的某种请求.从而实现远程调用.  1:从WebService的工作模式上 ...

随机推荐

  1. Eclipse之maven插件link方式安装

    maven是开发人员要具备的必不可少的技能之一.在使用eclipse进行开发时,我们需要安装maven插件,网上有很多教程,但是有些教程写的太过模糊.在此,我将自己的安装方法总结一下,尽量细致. 前提 ...

  2. checkbox勾选事件,JQ设置css,下拉框JQ选中

    <input id="CheckMainCompany" type="checkbox"/> $(function() { $("#Che ...

  3. 展讯7731C_M Android6.0 充电指示灯实现(一)------关机充电实现【转】

    本文转载自:https://blog.csdn.net/m0_37870649/article/details/80566131 前言: 在手机充电中常常使用充电指示灯来观察手机充电状态,比如说将手机 ...

  4. 配置Codeblocks

    1.安装mingw 官网:http://www.mingw.org/找到左边Navigation里面的Download(选择下图第一个即可,安装管理器,可自己手动选择要安装的组件)下图是安装管理器界面 ...

  5. C# winform程序防止前台卡死

    https://blog.csdn.net/Emiedon/article/details/51069193 在实际开发中,如果需要实时的显示后台处理的情况,我们可能要在前台用一些控件去显示 所以我们 ...

  6. 题解——洛谷P2827 NOIP提高组 2016 蚯蚓

    队列模拟 详细题解待填坑 #include <cstdio> #include <algorithm> #include <queue> #include < ...

  7. 深度学习课程笔记(十三)深度强化学习 --- 策略梯度方法(Policy Gradient Methods)

    深度学习课程笔记(十三)深度强化学习 --- 策略梯度方法(Policy Gradient Methods) 2018-07-17 16:50:12 Reference:https://www.you ...

  8. x=x+1,x+=1,及x++的效率哪个最高,为什么?

    x=x+1: 1.     读取右X的地址 2.     X+1 3.     读取左X的地址 4.     将右值传给左边的X x+=1: 1.     读取右边的x的地址 2.     X+1 3 ...

  9. C#题目及答案(1)

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成 ...

  10. iPhone 尺寸 iPhonex

    http://tool.lanrentuku.com/guifan/ui.html 这是本人复制的链接,,如有不适用,,请;联系本人删除链接,,谢谢. iPhone x尺寸 1125x2436@3x ...