WebService上传和下载文件
来源:互联网 发布:java内嵌webkit浏览器 编辑:程序博客网 时间:2024/05/01 01:42
/*上传文件的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上传和下载文件
- 通过WebService上传和下载文件
- 通过WebService 上传和下载文件
- WebService文件上传,下载
- 利用WebService实现远程服务器文件的上传和下载
- 利用WebService实现远程服务器文件的上传和下载
- CXF webService 实现文件上传与下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- css绘制带箭头对话框
- hibernate关于session的关闭问题
- SAP 网络流模板,邻接矩阵
- ios 获取设备号
- python编程常用模板总结
- WebService上传和下载文件
- python基础学习二 list和tuple(列表和元组)
- Hbernate一对多和多对一关系详解
- 一路PN码串行捕获设计--基于《通信收发信机的verilog实现与仿真》实例
- select into from 和 insert into select 的用法和区别复制表数据
- Android横竖屏切换小结
- Cocos2d-x shader学习2: 模糊(Blur)
- 项目管理-常用工具推荐
- echarts报错ReferenceError: require is not defined