WebService项目中的Web.config配置代码
<appSettings>
<add key="UploadFileFolder" value="/Uploads/TestUpload/" />
</appSettings>
WebService项目中的ImageService.asmx代码
/// <summary>
/// Webservice中的下载文件处理函数
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns>返回文件流</returns>
[WebMethod(Description = "下载服务器站点文件,传递文件相对路径")]
public byte[] DownloadFile(string strFilePath)
{
FileStream fs = null;
string CurrentUploadFolderPath = Server.MapPath(ConfigurationManager.AppSettings["UploadFileFolder"]);
string CurrentUploadFilePath = CurrentUploadFolderPath + strFilePath;
if (File.Exists(CurrentUploadFilePath))
{
try
{
///打开现有文件以进行读取。
fs = File.OpenRead(CurrentUploadFilePath);
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1 = fs.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
}
catch (Exception ex)
{
return new byte[0];
}
finally
{
fs.Close();
}
}
else
{
return new byte[0];
}
}
Winform项目中的窗体下载按钮代码
private void btnDownload_Click(object sender, EventArgs e)
{
string CurrentServiceFilePath = this.txtServiceFile.Text.Trim();
string CurrentDownloadFolderPath = this.txtDownloadFolder.Text.Trim();
if (CurrentServiceFilePath == "" || CurrentDownloadFolderPath == "")
{
MessageBox.Show(DownloadImage(CurrentServiceFilePath, CurrentDownloadFolderPath));
}
else if (CurrentServiceFilePath == "")
{
MessageBox.Show("请填写要下载的服务器文件路径和选择本地保存目录");
}
else if (CurrentDownloadFolderPath == "")
{
MessageBox.Show("请填写要下载的服务器文件路径和选择本地保存目录");
}
}
Winform项目中的窗体下载按钮调用函数
/// <summary>
/// 通过WebService下载文件
/// </summary>
/// <param name="ServiceFilePath">服务器图片路径</param>
/// <param name="DownloadFolderPath">本地图片路径</param>
private string DownloadImage(string ServiceFilePath, string DownloadFolderPath)
{
try
{
string DownloadFileName="";
if (ServiceFilePath.Contains("/"))
{
DownloadFileName=ServiceFilePath.Substring(ServiceFilePath.LastIndexOf("/"));
}
else
{
DownloadFileName = ServiceFilePath;
}
string DownloadFilePath = DownloadFolderPath +"\\"+ DownloadFileName;
localhost.ImageService myImageService=new localhost.ImageService();
byte[] bytes = myImageService.DownloadFile(ServiceFilePath);
if (bytes != null)
{
if (!Directory.Exists(DownloadFolderPath)) {
Directory.CreateDirectory(DownloadFolderPath);
}
if (!File.Exists(DownloadFilePath))
{
File.Create(DownloadFilePath).Dispose();
}
//如果不存在完整的上传路径就创建
FileInfo downloadInfo = new FileInfo(DownloadFilePath);
if (downloadInfo.IsReadOnly) { downloadInfo.IsReadOnly = false; }
//定义并实例化一个内存流,以存放提交上来的字节数组。
MemoryStream ms = new MemoryStream(bytes);
//定义实际文件对象,保存上载的文件。
FileStream fs = new FileStream(DownloadFilePath, FileMode.Create);
///把内内存里的数据写入物理文件
ms.WriteTo(fs);
fs.Flush();
ms.Flush();
ms.Close();
fs.Close();
fs = null;
ms = null;
}
return "下载成功";
}
catch(Exception ex)
{
return "下载失败"+ex.Message;
}
}
分享到:
相关推荐
用webservice作为服务器端,以流的方式实现文件的上传和下载,可以自动调用winrar进行压缩和加密,支持多线程和断点续传功能,默认是16K一个包,全部源码,已经在实际项目中应用。客户端有winform和服务两种方式的...
C# Webservice返回指定的xml格式,使用VS2019.本文介绍了通过C#Web服务返回XML文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧
C# WebService返回数据四种方法(DataSet, 序列化的DataSet, Microsoft组件处理序列化DataSet, Microsoft组件处理序列化压缩DataSet)及动态调用WebService
webservice的上传、下载、获取文件名称数量,完整代码 可直接运行
搭建iis网站,通过webservice指定网址,即可通过调用webservice访问数据库。
Android 上传文件到c# webservice 这是客户的 服务的的代码咋在:http://blog.sina.com.cn/s/blog_8d71d5140101fk8r.html
Csharp winfrom中webservice接口连接服务器上传图片和下载图片
delphi+C#webservice上传文件
C#WebService发布和IIS配置以及部分问题解决
经过一上午的网上查找,实现的C#写的,Webservice接收微信小程序上传的图片文件,有单个图片的上传代码,也有多个图片的上传代码,包括网页木马验证等程序
WebService上传、下载显示图片,有需要的下载! 解压密码:www.cnblogs.com/xvqm00
请参考:http://blog.csdn.net/dwj901125/article/details/23701063 这里有相当详尽的实现过程。
开发webservice可以使用java、也可以使用c#。由于国内大多数计算机安装的是Windows系列操作系统,所以使用c#开发webservice不仅开发效率高,而且部署和维护很方便。 本文详细介绍了java如何调用c#开发的webservice。...
Webservice方式的文件上传和下载,采用c#开发,代码说明详细说明了配置,并解决大文件上传的异常处理。
C#调用WebService实例和开发,对于初学者来说,在vs2012环境中,大家有可能不知道Web References文件夹是如何来的,它是通过右击项目,选择其中的”添加服务引用“。
Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI...
主要功能是图片数据录入...数据库文件EmotionalWebService\MySolution.WebService\App_Data中。开发工具是Visual Studio 2008,语言C#,数据库采用SQL Server 2005。附带所有源码以及说明文档,谢谢。
java调用c#的webservice,其中包括demo和需要的jar包,下载下来,导入eclipse就可以直接看到main方法。不需要配置什么文件
C# _.NET_动态调用webservice的三种方式 在本文中,我们将讨论如何动态调用WebService的三种方式。在某些情况下,我们可能需要在程序运行期间动态调用一个未知的服务。这可以通过使用.NET Framework的System.Web....