环境:.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. UIGestureRecognizer ios手势识别温习

    1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性. iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了 ...

  2. CNN 美国有线电视新闻网 wapCNN WAP 指无线应用通讯协议 ---- 美国有线电视新闻网 的无线应用

    wapCNN  wap指无线应用通讯协议  CNN美国有线电视新闻网   固, wapCNN 美国有线电视新闻网的无线应用 -------------------------------------- ...

  3. ASP.NET 5 :上传文件(转)

    (此文章同时发表在本人微信公众号“dotNET每日精华文章”,欢迎右边二维码来关注.) 题记:在ASP.NET 5(MVC 6)中处理上传文件的方式和之前有所不同. 在MVC 5之前的版本中上传文件, ...

  4. TYVJ2477 架设电话线

    题目描述 Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用.     FJ的农场周围分布着N(1 <= N < ...

  5. 修改 ~/.bashrc显示 git 当前分支

    vim ~/.bashrc # git branch show configuration PS1="\\w:\$(git branch 2>/dev/null | grep '^*' ...

  6. CreateRemoteThread远程线程注入Dll与Hook

    CreateRemoteThread虽然很容易被检测到,但是在有些场合还是挺有用的.每次想用的时候总想着去找以前的代码,现在在这里记录一下. CreateRemoteThread远程注入 DWORD ...

  7. postgresql 函数demo

    create or replace function refresh_product_usage() returns void as $$ declare rec record; sub_rec re ...

  8. PHP中冒号、endif、endwhile、endfor这些都是什么

    我们经常在wordpress一类博客程序的模板里面看到很多奇怪的PHP语法,比如:<?php if(empty($GET_['a'])): ?><font color="r ...

  9. Html form 表单提交前验证

    可以使用form表单的onsubmit方法,在提交表单之前,对表单或者网页中的数据进行检验. onsubmit指定的方法返回true,则提交数据:返回false不提交数据. 直接看下面的代码: 1 & ...

  10. win7系统扩展双屏幕时,如何在两个屏幕下都显示任务栏

    扩展屏幕下都显示任务栏!!! win7系统本身无法设置该功能(目前我是不知道) 但可以下载第三方软件来解决该问题. 第一步:Dual Monitor Taskbar 下载软件 下载链接:http:// ...