WebService中实现上传下载文件

不多说,直接看代码:

/*上传文件的WebService*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.IO;

/// <summary>
/// Upload 的摘要说明。
/// </summary>
[WebService(Namespace = "http://tempuri.org/",
Description = "在Web Services里利用.NET框架进上载文件。")]
public class UploadFile : System.Web.Services.WebService
{
public UploadFile()
{
}

[WebMethod(Description = "Web 服务提供的方法,返回是否文件上载成功与否。")]
public string Upload(byte[] fs, string fileType)
{
string FileName = System.DateTime.Now.ToString("yyyyMMddHHmmssms") +"." +fileType;
try
{
///定义并实例化一个内存流,以存放提交上来的字节数组。
MemoryStream m = new MemoryStream(fs);
///定义实际文件对象,保存上载的文件。
FileStream f = new FileStream(Server.MapPath(".") + "\\UploadFile\\"
+ FileName, FileMode.Create);
///把内内存里的数据写入物理文件
m.WriteTo(f);
m.Close();
f.Close();
f = null;
m = null;
return FileName;
}
catch (Exception ex)
{
return null;
}
}

[WebMethod(Description = "Web 服务提供的方法,删除指定文件")]
public void Delete(string fileName)
{
string filePath = Server.MapPath(".") + "\\UploadFile\\" + fileName;
File.Delete(filePath);
}
}

/*下载文件*/

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;

/// <summary>
/// GetBinaryFile 的摘要说明。
/// Web Services名称:GetBinaryFile
/// 功能:返回服务器上的一个文件对象的二进制字节数组。
/// </summary>
[WebService(Namespace = "http://tempuri.org/",
Description = "在Web Services里利用.NET框架进行传递二进制文件。")]
public class GetBinaryFile : System.Web.Services.WebService
{
/// <summary>
/// Web 服务提供的方法,返回给定文件的字节数组。
/// </summary>
[WebMethod(Description = "Web 服务提供的方法,返回给定文件的字节数组")]
public byte[] GetFile(string requestFileName)
{
string fileName = Server.MapPath(".") + "\\UploadFile\\" + requestFileName;
return getBinaryFile(fileName);
}

/// <summary>
/// getBinaryFile:返回所给文件路径的字节数组。
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public byte[] getBinaryFile(string filename)
{
if (File.Exists(filename))
{
try
{
///打开现有文件以进行读取。
FileStream s = File.OpenRead(filename);
return ConvertStreamToByteBuffer(s);
s.Close();
}
catch (Exception e)
{
return new byte[0];
}
}
else
{
return new byte[0];
}
}

/// <summary>
/// ConvertStreamToByteBuffer:把给定的文件流转换为二进制字节数组。
/// </summary>
/// <param name="theStream"></param>
/// <returns></returns>
public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1 = theStream.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();

}

[WebMethod(Description = "Web 服务提供的方法,返回给定文件类型。")]
public string GetImageType()
{
///这里只是测试,您可以根据实际的文件类型进行动态输出
return "image/jpg";
}
}

/*调用上传*/

string filePath = "c:\test.jpg";
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate,
FileAccess.Read);
// Read the Data into the Byte Array
byte[] fileByte = new byte[fs.Length];
fs.Read(fileByte, 0, (int)fs.Length);

UploadFile uploadfile = new UploadFile();
int indexof = filePath.LastIndexOf(".")+1;
string fileExt = filePath.Substring(indexof, filePath.Length - indexof);
string filename = uploadfile.Upload(fileByte, fileExt);

WebService中实现上传下载文件

时间: 2024-08-01 19:59:57

WebService中实现上传下载文件的相关文章

[转]JSP或servlet中(以及上传下载文件)中文乱码或不显示的解决方案

时间 2014-04-14 14:33:44  CSDN博客 原文  http://blog.csdn.net/xby1993/article/details/23677375 主题 ServletJSP JSP或servlet中中文乱码的解决方案: 1.以POST请求方式: 在获取请求参数之前设置: request.setCharacterEncoding("utf-8"); 设置输出编码: response.setContentType("text/html;charse

2.1.5基础之命令行链接ftp dos中的ftp上传下载文件

Windows命令行batcmd脚本的应用之自动备份 异地备份2.1.5基础之命令行链接ftp dos中的ftp上传下载文件 讲解环境 VMware Workstation 12 桌面虚拟计算机软件创建虚拟机安装操作系统:http://edu.51cto.com/course/10007.html PC1:192.168.1.201 远程地址:192.168.100.100:2001 windows service2008 pc1 Admin111FTP虚拟用户 fileaa fileaaPC2

Linux上传下载文件

2种方式:xftp(工具).lrzsz xftp:协议--SFTP.端口号--22 lrzsz: rz,sz是Linux/Unix同Windows进行ZModem文件传输的命令行工具. 优点就是不用再开一个sftp工具登录上去上传下载文件. sz(下载):将选定的文件发送(send)到本地机器 rz(上传):运行该命令会弹出一个文件选择窗口,从本地选择文件上传到Linux服务器 安装命令:yum install lrzsz 从服务端发送文件到客户端:sz filename 从客户端上传文件到服务

向云服务器上传下载文件方法汇总(转)

转载于:https://yq.aliyun.com/articles/64700 摘要: 一.向Windows服务器上传下载文件方式 方法有很多种,此处介绍远程桌面的本地资源共享方法. 1.运行mstsc,连接远程桌面的时候,点"选项>>" 2."本地资源"-->详细信息. 3."磁盘驱动器"前面打钩. 一.向Windows服务器上传下载文件方式 方法有很多种,此处介绍远程桌面的本地资源共享方法. 1.运行mstsc,连接远程桌

向linux服务器上传下载文件方式收集

向linux服务器上传下载文件方式收集 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法] scp就是secure copy,是用来进行远程文件拷贝的.数据传输使用 ssh,并且和ssh 使用相同的认证方式,提供相同的安全保证 . 命令格式: scp [参数] <源地址(用户名@IP地址或主机名)>:<文件路径> <目的地址(用户名 @IP 地址或主机名)>:<文件路径> 举例: scp /home/work/source.

rz和sz上传下载文件工具lrzsz

######################### rz和sz上传下载文件工具lrzsz ############################################################ 1 rpm -qa |grep lrzsz 如果没有用RPM安装即可: 2 rpm -ivh lrzsz-0.12.20-27.1.el6.x86_64.rpm (去光盘里找) 或者 yum install -y lrzsz 即可. ###########################

winform通过FTP协议上传下载文件

上传文件:窗体代码 一次上传多个文件(grdAffixFilesList中需要上传的) private Boolean UploadFile() { string filename; int upCount=0; for (int i = 0; i < this.grdAffixFilesList.Rows.Count; i++) { filename = this.grdAffixFilesList.Rows[i].Cells["FILEPATH"].Text.ToString

[Linux]非常方便的上传下载文件工具rz和sz

?? linux上非常方便的上传下载文件工具rz和sz (本文适合linux入门的朋友) [一般用于SecureCRT ssh中使用] █ 法一:直接用yum安装lrzsz(推荐) yum install lrzsz -y 注意:rhel安装完系统后 直接可使用 rz sz 用法: 1).上传文件: rz 本地文件名称 rz -y 本地文件名称                       覆盖上传 2).下载文件: sz remotefilename sz -y remotefilename  

经验分享] 向云服务器上传下载文件方法汇总

一.向Windows服务器上传下载文件方式 方法有很多种,此处介绍远程桌面的本地资源共享方法. 1.运行mstsc,连接远程桌面的时候,点"选项>>" <ignore_js_op> 2."本地资源"-->详细信息. <ignore_js_op> 3."磁盘驱动器"前面打钩. <ignore_js_op> 4.连接远程电脑后,打开"我的电脑",就能看到刚刚共享的硬盘驱动器.把