环境:.NET Framework 3.5

服务: IIS EXpress托管 WCF服务程序

配置:Web.config

<!--<connectionStrings>
<add name="DbConnection"
connectionString="server=数据库地址;database=数据库名字;uid=用户名;pwd=密码;"/>
</connectionStrings>-->

  <system.serviceModel>
<services>
<service name="TestWcfService.Service1" behaviorConfiguration="TestWcfService.Service1Behavior">
<!-- Service Endpoints -->
<!--<endpoint address="" binding="basicHttpBinding" contract="TestWcfService.IService1" bindingConfiguration="LargeBuffer">-->
<endpoint address="http://IP地址:端口号/PhoneApp/Service1.svc" binding="basicHttpBinding" contract="TestWcfService.IService1" bindingConfiguration="LargeBuffer">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="TestWcfService.Service1Behavior">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
<dataContractSerializer maxItemsInObjectGraph="" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="LargeBuffer"
maxBufferSize=""
maxBufferPoolSize=""
maxReceivedMessageSize="">
<readerQuotas
maxStringContentLength=""
maxBytesPerRead=""
maxDepth=""
maxNameTableCharCount=""
maxArrayLength="" />
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>

Web.Debug.config and Web.Release.config

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="DbConnection"
connectionString="server=数据库地址;database=数据库名字;uid=用户名;pwd=密码;"/>
</connectionStrings>
<system.web>
</system.web>
</configuration>

说明:

本机调试程序的时候 打开Web.config 文件 绿色的标注的connectionStrings 和 endpoint address 节点 注销 红色的 endpoint address节点。调试成功

如果将WCF服务程序发布到网站的时候,关闭Web.config 文件 绿色的标注的connectionStrings 和 endpoint address 节点 打开 红色的 endpoint address节点。调试成功

为什么这么做:

客户端调用发布后的WCF服务接口的时候,有时候会出现 时而断时而连的情况。注明

<endpoint address="http://IP地址:端口号/PhoneApp/Service1.svc" binding="basicHttpBinding" contract="TestWcfService.IService1" bindingConfiguration="LargeBuffer">

这个节点说明是要访问指定的服务就不会出现 访问远程服务没有找到这个问题了 更新WCF服务的时候,要在客户端更新WCF服务,引用的就是新服务

配置好WCF服务程序在WCF服务程序里实现2步

1.上传图片到服务器 IService1.cs Service1.svc

namespace TestWcfService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
bool StationUpFile(UpFileContent Date);
}
public class UpFileContent
{
public byte[] ImageContent { get; set; }
}
}
        public bool StationUpFile(UpFileContent Date)
{
bool IsSuccess = false;
#region UpImage string DiskName = "e:";
string FileAddress = @"\ProductImages\Larger\";
string LocationAddress = DiskName + FileAddress;
string filePath = string.Empty;
using (Stream sourceStream = new MemoryStream(Date.ImageContent))
{
if (!sourceStream.CanRead)
{
throw new Exception("");
}
if (!Directory.Exists(LocationAddress))
{
Directory.CreateDirectory(LocationAddress);
}
filePath = Path.Combine(LocationAddress, Date.StationImageName);
if (!File.Exists(filePath))
{
try
{
using (FileStream targetStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
{
int count = ;
const int bufferlength = ;
byte[] buffer = new byte[bufferlength];
while ((count = sourceStream.Read(buffer, , bufferlength)) > )
{
targetStream.Write(buffer, , count);
}
IsSuccess = true;
}
}
catch
{
return IsSuccess;
}
}
} LocationAddress = @"e:\ProductImages\Middle\";
if (!Directory.Exists(LocationAddress))
Directory.CreateDirectory(LocationAddress); MakeSmallImg(filePath, Path.Combine(LocationAddress, Date.StationImageName), , ); LocationAddress = @"e:\ProductImages\Small\";
if (!Directory.Exists(LocationAddress))
Directory.CreateDirectory(LocationAddress);
MakeSmallImg(filePath, Path.Combine(LocationAddress, Date.StationImageName), , );
#endregion
       
      //这里实现将图片名字写到数据库的某个字段里
      
}

此路径就是服务器的路径 (因为WCF服务程序部署到该服务器啦!写文件就写到这个服务器的路径下)

这是分成 大图 中图 小图的函数 可以略过

        private void MakeSmallImg(string filePath, string fileSaveUrl, System.Double templateWidth, System.Double templateHeight)
{
using (Image myImage = System.Drawing.Image.FromFile(filePath))
{
System.Double newWidth = myImage.Width, newHeight = myImage.Height;
//宽大于模版的横图
if (myImage.Width > myImage.Height || myImage.Width == myImage.Height)
{
if (myImage.Width > templateWidth)
{
//宽按模版,高按比例缩放
newWidth = templateWidth;
newHeight = myImage.Height * (newWidth / myImage.Width);
}
}
//高大于模版的竖图
else
{
if (myImage.Height > templateHeight)
{
//高按模版,宽按比例缩放
newHeight = templateHeight;
newWidth = myImage.Width * (newHeight / myImage.Height);
}
}
System.Drawing.Size mySize = new System.Drawing.Size((int)newWidth, (int)newHeight);
using (Image bitmap = new System.Drawing.Bitmap(mySize.Width, mySize.Height))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.InterpolationMode = InterpolationMode.High;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.Clear(Color.Transparent);
graphics.DrawImage(myImage, new System.Drawing.Rectangle(, , bitmap.Width, bitmap.Height), new System.Drawing.Rectangle(, , myImage.Width, myImage.Height), System.Drawing.GraphicsUnit.Pixel);
bitmap.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}

2.将编译好的WCF服务程序 放到该网站。

接下来 要在wp客户端程序 更新引用服务

wp 客户端程序调用WCF服务

        ServiceReference1.Service1Client Sc;
public void Post(Action<bool> action)
{
//若用户选择了图片,则实例化 UploadPic 对象,用于上传图片
//注意:必须在UI线程实例化该对象! new Thread(() =>
{
Sc = new ServiceReference1.Service1Client();
Sc.OpenAsync();
  
if (null != ImageSource)
{
MemoryStream fileStream = new MemoryStream();
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream = store.OpenFile(pic.FullPathName, FileMode.Open))
{
isoStream.CopyTo(fileStream);
}
}
//流转换为字节数组
byte[] tbuffer = fileStream.ToArray();
UpFileContent upfile = new UpFileContent();
upfile.StationImageName = pic.FileName;
upfile.ImageContent = tbuffer; try
{
Sc.StationUpFileAsync(upfile);
Sc.StationUpFileCompleted += (s1, e1) =>
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{ if (e1.Result)
{
fileStream.Close();
fileStream.Dispose();
}
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if (null != action)
{
Sc.CloseAsync();
ImageSource = null;
action(true);
}
});
});
};
}
catch (TimeoutException timeout)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if (null != action)
{
Sc.Abort();
ImageSource = null;
MessageBox.Show(timeout.Message);
action(false);
}
});
}
catch (CommunicationException commException)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if (null != action)
{
Sc.Abort();
MessageBox.Show(commException.Message);
ImageSource = null;
action(false);
}
});
}
}
else
{ } }).Start();
}

THE END

WCF服务发布程序 到此告一段落!感谢!

Wcf for wp8 上传图片到服务器,将图片名字插入数据库字段(五)的更多相关文章

  1. PHP部分--图片上传服务器、图片路径存入数据库,并读取

    html页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  2. PHP部分--file图片上传服务器、图片路径存入数据库,并读取

    前端代码 <form action="shangchuan.php" method="post" enctype="multipart/form ...

  3. java后台中处理图片辅助类汇总(上传图片到服务器,从服务器下载图片保存到本地,缩放图片,copy图片,往图片添加水印图片或者文字,生成二维码,删除图片等)

    最近工作中处理小程序宝箱活动,需要java画海报,所以把这块都快百度遍了,记录一下处理的方法,百度博客上面也有不少坑! 获取本地图片路径: String bgPath = Thread.current ...

  4. 小程序踩坑记录-上传图片及canvas裁剪图片后上传至服务器

    最近在写微信小程序的上传图片功能,趟过了一些坑记录一下. 想要满足的需求是,从手机端上传图片至服务器,为了避免图片过大影响传输效率,需要把图片裁剪至适当大小后再传输 主要思路是,通过wx.choose ...

  5. 通过android 客户端上传图片到服务器

    昨天,(在我的上一篇博客中)写了通过浏览器上传图片到服务器(php),今天将这个功能付诸实践.(还完善了服务端的代码) 不试不知道,原来通过android 向服务端发送图片还真是挺麻烦的一件事. 上传 ...

  6. Android 上传图片到服务器 okhttp一

    [目录] (一)上传图片到服务器一 ---------------------------------Android代码 (二)上传图片到服务器二--------------------------- ...

  7. c#批量上传图片到服务器示例分享

    这篇文章主要介绍了c#批量上传图片到服务器示例,服务器端需要设置图片存储的虚拟目录,需要的朋友可以参考下 /// <summary> /// 批量上传图片 /// </summary ...

  8. 5分钟Serverless实践:构建无服务器的图片分类系统

    前言 在过去“5分钟Serverless实践”系列文章中,我们介绍了如何构建无服务器API和Web应用,从本质上来说,它们都属于基于APIG触发器对外提供一个无服务器API的场景.现在本文将介绍一种新 ...

  9. 改造vue-quill-editor: 结合element-ui上传图片到服务器

    前排提示:现在可以直接使用封装好的插件vue-quill-editor-upload 需求概述 vue-quill-editor是我们再使用vue框架的时候常用的一个富文本编辑器,在进行富文本编辑的时 ...

随机推荐

  1. hdu 1048 The Hardest Problem Ever

    import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(Strin ...

  2. LESSCSS的几点摘要

    字符串插值 变量可以用像 @{name} 这样的结构,以类似 ruby 和 php 的方式嵌入到字符串中: @base-url: "http://assets.fnord.com" ...

  3. ReactiveCocoa初步

    [self.usernameTextField.rac_textSignal subscribeNext:^(id x) { NSLog(@"%@", x); }]; 打印结果 - ...

  4. php中文字符串翻转

    转自:http://www.oschina.net/code/snippet_613962_17070 <?php header("content-type:text/html;cha ...

  5. java开发微信公众平台备忘

    简单记录下前段时间开发的电子书的 公众平台的一些备忘及开发心得经验等 eclipse的一些技巧: 1.ctrl+shift+o 自动添加必要import空间及移除无用import 项目备忘+说明 1. ...

  6. 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1

    注:这里inc方法和dec方法加synchronized关键字是因为当两个线程同时操作同一个变量时,就算是简单的j++操作时,在系统底层也是通过多条机器语句来实现,所以在执行j++过程也是要耗费时间, ...

  7. make -e install ,,,make命令的-e选项!

    -e, --environment-overrides Environment variables override makefiles.环境变量覆盖Makefile文件. 用这个时,一般都自己编写s ...

  8. js数组常用操作方法小结(增加,删除,合并,分割等)

    本文实例总结了js数组常用操作方法.分享给大家供大家参考,具体如下: var arr = [1, 2, 3, 4, 5]; //删除并返回数组中第一个元素 var theFirst = arr.shi ...

  9. js中的getAttribute方法使用示例

    getAttribute()方法是一个函数.它只有一个参数——你打算查询的属性的名字,下面为大家介绍下其具体的使用   getAttribute()方法 至此,我们已经向大家介绍了两种检索特定元素节点 ...

  10. web前端跨域方案

    ajax跨域请求   qzfl实现 跨子域的xhr 原生xhr不支持跨域,通过iframe+proxy.html达到跨子域 假如A页面要请求B页面,A.B跨子域.A创建指向B的proxy页的ifram ...