不多说,直接看代码:

/*上传文件的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[];
}
}
else
{
return new byte[];
}
} /// <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()) != -)
{
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, , (int)fs.Length); UploadFile uploadfile = new UploadFile();
int indexof = filePath.LastIndexOf(".")+;
string fileExt = filePath.Substring(indexof, filePath.Length - indexof);
string filename = uploadfile.Upload(fileByte, fileExt);

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

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

    时间 2014-04-14 14:33:44  CSDN博客 原文  http://blog.csdn.net/xby1993/article/details/23677375 主题 ServletJ ...

  2. 在windows中使用PuTTy上传下载文件和目录

    打开windows的cmd,使用cd命令切换到PuTTy安装目录 C:\Users\NUC>cd C:\Program Files\PuTTY 在cmd中使用pscp命令上传下载文件 windo ...

  3. java web service 上传下载文件

    1.新建动态web工程youmeFileServer,新建包com,里面新建类FileProgress package com; import java.io.FileInputStream; imp ...

  4. Javaweb学习笔记——上传下载文件

    一.前言 在Javaweb中,上传下载是经常用到的功能,对于文件上传,浏览器在上传的过程中是以流的过程将文件传给服务器,一般都是使用commons-fileupload这个包实现上传功能,因为comm ...

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

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

  6. linux上很方便的上传下载文件工具rz和sz

    linux上很方便的上传下载文件工具rz和sz(本文适合linux入门的朋友) ##########################################################&l ...

  7. linux下常用FTP命令 上传下载文件【转】

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...

  8. C#实现http协议支持上传下载文件的GET、POST请求

    C#实现http协议支持上传下载文件的GET.POST请求using System; using System.Collections.Generic; using System.Text; usin ...

  9. 初级版python登录验证,上传下载文件加MD5文件校验

    服务器端程序 import socket import json import struct import hashlib import os def md5_code(usr, pwd): ret ...

随机推荐

  1. Oracle数据库——触发器的创建与应用

    一.涉及内容 1.理解触发器的概念.作用和类型. 2.练习触发器的创建和使用. 二.具体操作 (实验) 1.利用触发器对在scott.emp表上执行的DML操作进行安全性检查,只有scott用户登录数 ...

  2. 在Windows上安装Maven

      下载 Maven 最新版本. http://maven.apache.org/download.cgi   1,下载包后,解压到相应特定位置. 2,将 [解压位置]/bin  加入到Path 3, ...

  3. 【shell】if语句

    单分支: #!/bin/bash rate=$(df -h|grep vg_andon-lv_root |awk '{print $5}'| cut -d "%" -f1) #ec ...

  4. [ZZ] MATLAB中Legend的一些控制方法

    http://www.eetop.cn/blog/html/03/6503-23349.html 如果一个图中我们画了n条曲线,但是我们只想加图例说明(legend)的只有m条 (m<n).网上 ...

  5. textarea 限制字数

    $("textarea").keyup(function(){        //console.log($(this).val().length);        var L=$ ...

  6. Linux From Scratch [2]

    1. gcc需要的一些lib GMP:A free library for arbitrary precision arithmetic, operating on signed integers, ...

  7. Linux下做软RAID

    1.查看有多少块硬盘可用#fdisk -l嗯,一般而言,留下系统盘不动,其它的盘如果大小相同的话,统统合起来做一个raid. 2.决定做什么类型的raid.raid-0.raid-1.raid-5?如 ...

  8. hist和bar画图关系

    1.hist是绘制直方图,直方图显示了数据值的分布情况.  1>n = hist(Y,n)      将向量Y中的元素分到n个等间隔的范围内(默认为10个间隔),并返回每个范围内元素的个数作为一 ...

  9. 冒泡算法C#

    冒泡算法C# namespace数组排序 { classProgram { staticvoidMain(string[]args) { inttemp=; ,,,,,,,,}; #region该段与 ...

  10. sql里将重复行数据合并为一行,数据用逗号分隔

    一.定义表变量 DECLARE @T1 table ( UserID int , UserName ), CityName ) ); ,'a','上海') ,'b','北京') ,'c','上海') ...