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[];
}
}
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中实现上传下载文件的更多相关文章
- [转]JSP或servlet中(以及上传下载文件)中文乱码或不显示的解决方案
时间 2014-04-14 14:33:44 CSDN博客 原文 http://blog.csdn.net/xby1993/article/details/23677375 主题 ServletJ ...
- 在windows中使用PuTTy上传下载文件和目录
打开windows的cmd,使用cd命令切换到PuTTy安装目录 C:\Users\NUC>cd C:\Program Files\PuTTY 在cmd中使用pscp命令上传下载文件 windo ...
- java web service 上传下载文件
1.新建动态web工程youmeFileServer,新建包com,里面新建类FileProgress package com; import java.io.FileInputStream; imp ...
- Javaweb学习笔记——上传下载文件
一.前言 在Javaweb中,上传下载是经常用到的功能,对于文件上传,浏览器在上传的过程中是以流的过程将文件传给服务器,一般都是使用commons-fileupload这个包实现上传功能,因为comm ...
- rz和sz上传下载文件工具lrzsz
######################### rz和sz上传下载文件工具lrzsz ####################################################### ...
- linux上很方便的上传下载文件工具rz和sz
linux上很方便的上传下载文件工具rz和sz(本文适合linux入门的朋友) ##########################################################&l ...
- linux下常用FTP命令 上传下载文件【转】
1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...
- C#实现http协议支持上传下载文件的GET、POST请求
C#实现http协议支持上传下载文件的GET.POST请求using System; using System.Collections.Generic; using System.Text; usin ...
- 初级版python登录验证,上传下载文件加MD5文件校验
服务器端程序 import socket import json import struct import hashlib import os def md5_code(usr, pwd): ret ...
随机推荐
- C# 大于屏幕的窗体
1.使用SetWindowPos就可以做到这一点,只是最后一个参数要选对. RECT windowRect = new RECT(); User32.GetWindowRect(MyForm2.Han ...
- ABBYY 识别结果的文档怎么导出
使用ABBYY FineReader Pro for Mac OCR文字识别软件识别文档时,识别结果可以保存至一个文件.复制到剪贴板或通过电子邮件发送.可以执行下列操作:导出整个文档.仅导出所选页面. ...
- linux工具之dracut
这是一个工具类,不是一个后台服务类 centos7.2-minimal就下面三个包 [root@1st-kvm ~]# rpm -qa|grep dracutdracut-config-rescue- ...
- 生成n个数的全排列【递归、回溯】
下面讨论的是n个互不相同的数形成的不同排列的个数.毕竟,假如n个数当中有相同的数,那n!种排列当中肯定会有一些排列是重复的,这样就是一个不一样的问题了. /*===================== ...
- python file operation
file.open(name[,mode[,buffering]]) 模式的类型有: r 默认只读 w 以写方式打开,如果文件不存在则会先创建,如果文件存在则先把文件内容清空(truncate ...
- 决策树模型组合之(在线)随机森林与GBDT
前言: 决策树这种算法有着很多良好的特性,比如说训练时间复杂度较低,预测的过程比较快速,模型容易展示(容易将得到的决策树做成图片展示出来)等.但是同时, 单决策树又有一些不好的地方,比如说容易over ...
- js 删除DropDownList的选项
function del_DropDownList_Option() { var ddlXZ= document.getElementById("name&quo ...
- (转)ORACLE触发器详解
本文转载自:http://blog.csdn.net/indexman/article/details/8023740/ ORACLE PL/SQL编程之八: 把触发器说透 本篇主要内容如下: 8.1 ...
- 【Spring-AOP-学习笔记-7】@Around增强处理简单示例
阅读目录 简单介绍 章节1:项目结构 章节2:定义切面类.连接点注解类 章节3:为待增强的方法--添加注解声明 章节4:AspectJ配置文件 章节5:测试类xxx 章节6:测试结果 Around 增 ...
- Redis集群方案介绍
由于Redis出众的性能,其在众多的移动互联网企业中得到广泛的应用.Redis在3.0版本前只支持单实例模式,虽然现在的服务器内存可以到100GB.200GB的规模,但是单实例模式限制了Redis没法 ...