.net 写文件上传下载webservice
2013-04-11 10:32
531 查看
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;
using System.Text;
namespace UpDownFile
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class UpDownFile : System.Web.Services.WebService
{
private static string filesavepath = System.Configuration.ConfigurationManager.AppSettings.Get("filepath");
/// <summary>
/// 下载文件
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
[WebMethod]
public bool DownFile(string filename,out byte[] bytes)
{
string filepath = filesavepath + filename;
LogWrite(filepath);
if (File.Exists(filepath))
{
try
{
FileStream s = File.OpenRead(filepath);
bytes = ConvertStreamToByteBuffer(s);
s.Close();
return true;
}
catch
{
bytes = new byte[0];
return false;
}
}
else
{
bytes = new byte[0];
return false;
}
}
public byte[] ConvertStreamToByteBuffer(Stream s)
{
MemoryStream ms = new MemoryStream();
int b;
s.Seek(0, 0);
while ((b = s.ReadByte()) !=-1)
{
ms.WriteByte((byte)b);
}
return ms.ToArray();
}
[WebMethod]
public bool UpFile(byte[] data, string filepath, string filename)
{
try
{
filepath = filesavepath + filepath;
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
}
if (Directory.Exists(filepath))
{
string fullname = filepath + filename;
if (File.Exists(fullname))
{
File.Delete(fullname);
}
FileStream fs = File.Create(fullname);
fs.Write(data, 0, data.Length);
fs.Close();
return true;
}
else return false;
}
catch
{
return false;
}
}
protected bool LogWrite( string content)
{
try
{
//读取
string file = "C:\\11111.txt";
StreamReader sr = new StreamReader(file, true);
string s = sr.ReadLine();
sr.Close();
//写入 与源文件字符相加
StreamWriter sw = new StreamWriter(file, true, Encoding.UTF8);
sw.WriteLine(s + content);
sw.Close();
return true;
}
catch (Exception ex)
{
//Response.Write(ex.Message.ToString());
return false;
}
}
}
}
注意:一定要加 s.Seek(0, 0);否则下载回来的文件数据会有丢失。
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;
using System.Text;
namespace UpDownFile
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class UpDownFile : System.Web.Services.WebService
{
private static string filesavepath = System.Configuration.ConfigurationManager.AppSettings.Get("filepath");
/// <summary>
/// 下载文件
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
[WebMethod]
public bool DownFile(string filename,out byte[] bytes)
{
string filepath = filesavepath + filename;
LogWrite(filepath);
if (File.Exists(filepath))
{
try
{
FileStream s = File.OpenRead(filepath);
bytes = ConvertStreamToByteBuffer(s);
s.Close();
return true;
}
catch
{
bytes = new byte[0];
return false;
}
}
else
{
bytes = new byte[0];
return false;
}
}
public byte[] ConvertStreamToByteBuffer(Stream s)
{
MemoryStream ms = new MemoryStream();
int b;
s.Seek(0, 0);
while ((b = s.ReadByte()) !=-1)
{
ms.WriteByte((byte)b);
}
return ms.ToArray();
}
[WebMethod]
public bool UpFile(byte[] data, string filepath, string filename)
{
try
{
filepath = filesavepath + filepath;
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
}
if (Directory.Exists(filepath))
{
string fullname = filepath + filename;
if (File.Exists(fullname))
{
File.Delete(fullname);
}
FileStream fs = File.Create(fullname);
fs.Write(data, 0, data.Length);
fs.Close();
return true;
}
else return false;
}
catch
{
return false;
}
}
protected bool LogWrite( string content)
{
try
{
//读取
string file = "C:\\11111.txt";
StreamReader sr = new StreamReader(file, true);
string s = sr.ReadLine();
sr.Close();
//写入 与源文件字符相加
StreamWriter sw = new StreamWriter(file, true, Encoding.UTF8);
sw.WriteLine(s + content);
sw.Close();
return true;
}
catch (Exception ex)
{
//Response.Write(ex.Message.ToString());
return false;
}
}
}
}
注意:一定要加 s.Seek(0, 0);否则下载回来的文件数据会有丢失。
相关文章推荐
- 利用webservice和.net技术上传和下载文件--Sending files in chunks with MTOM Web Services and .NET 2.0 from:http://www.codeproject.com/soap/MTO
- .net 写文件上传下载webservice
- CXF-webService实现文件上传下载
- WebService 实现文件的上传下载(非自动生成)
- .Net 2.0对文件传输协议(FTP)操作(上传,下载,新建,删除,FTP间传送文件等) 2
- 利用WebService实现远程服务器文件的上传和下载
- .Net 无法打开Offie Open XML文件(上传和下载使用)
- .NET中实现文件的上传与下载
- webservice 上传下载 大文件
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- 使用.net FtpWebRequest 实现FTP常用功能 上传 下载 获取文件列表 移动 切换目录 改名 .
- .NET实现文件跨服务器上传下载的方法
- iOS开发之结合asp.net webservice实现文件上传下载
- .net文件上传及下载
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- [.Net码农]将文件上传到数据库 和 从数据库下载文件到本地
- 利用WebService实现远程服务器文件的上传和下载
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载