.NET企业级应用WebService上传下载文件
在建立好一个WebService后会有一个自带的
[WebMethod]//在待会写的所有方法中都要写这个,便于调试
public string HelloWorld()
{
return "Hello World";
}
现在可以试一下录入记录
[WebMethod]
public UserInfo Login(string userName, string pwd)
{
if (userName == "admin" && pwd == "")
{
return new UserInfo() { UserName="admin",Pwd="",Age=,Remark="我很帅" };
}
else
{
return null;
}
}
在MVC项目中的控制器中调用
//第一步:添加服务引用
//实例化服务引用:服务对象以SoapClient
MyWebServiceSoapClient client = new MyWebServiceSoapClient();
public ActionResult Index()
{
string result = client.HelloWorld();
Response.Write(result);
return View();
} [WebMethod]
public UserInfo Login(string userName, string pwd)
{
if (userName == "admin" && pwd == "123")
{
return new UserInfo() { UserName="admin",Pwd="",Age=50,Remark="我很帅" };
}
else
{
return null;
}
}
然后可以写简单的文件上传下载
public class MyWebService : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="bytes"></param>
/// <param name="fileName"></param>
/// <returns></returns>
[WebMethod]
public bool FileUpload(byte[] bytes,string fileName)
{
try
{
//实例化内存对象MemoryStream,将byte数组装入内存对象
MemoryStream memory = new MemoryStream(bytes);
//文件保存路径
string filePath = Server.MapPath("~/Files/" + fileName);
//实例化文件对象
FileStream fStream = new FileStream(filePath, FileMode.OpenOrCreate);
//将内存对象写入文件对象
memory.WriteTo(fStream);
//释放对象
memory.Close();
memory.Dispose();
fStream.Close();
fStream.Dispose();
return true;
}
catch (Exception ex)
{
return false;
}
} [WebMethod]
public byte[] FileDownLoad(string FileName)
{
//加载路径
string filePath = Server.MapPath("~/Files/" + FileName);
//实例化文件对象,并读取指定的文件
FileStream fs = File.OpenRead(filePath);
int b1;
//实例化内存对象
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
//循环读取文件并将文件转换为byte[]
while ((b1 = fs.ReadByte()) != -)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
} [WebMethod]
public List<FileManager> GetFileList()
{
//加载路径
string filePath = Server.MapPath("~/Files/");
//实例化DirectoryInfo并加载指定路径
DirectoryInfo directory = new DirectoryInfo(filePath);
List<FileManager> result = new List<FileManager>(); //便利指定路径下的所有文件夹
foreach (DirectoryInfo item in directory.GetDirectories())
{
FileManager temp = new FileManager();
temp.FileName = item.Name;
temp.FilePath = item.FullName;
temp.FileType = ;
result.Add(temp);
}
//遍历指定路径下的所有文件
foreach (FileInfo item in directory.GetFiles())
{
FileManager temp2 = new FileManager();
temp2.FileName = item.Name;
temp2.FilePath = item.FullName;
temp2.FileType = ;
result.Add(temp2);
}
return result;
}
}
在在MVC项目中的控制器中调用
public ActionResult FileUpload()
{
return View();
} [HttpPost]
public ActionResult FileUpload(HttpPostedFileBase file1)
{
Stream fileStream = file1.InputStream;
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, , bytes.Length);
// 设置当前流的位置为流的开始
fileStream.Seek(, SeekOrigin.Begin);
bool result = client.FileUpload(bytes, file1.FileName);
if (result)
{
Response.Write("文件上传成功!");
}
else
{
Response.Write("文件上传失败!");
}
return View();
} public ActionResult FileDownLoad()
{
List<FileManager> result = client.GetFileList().ToList();
StringBuilder sb = new StringBuilder();
sb.Append("<ul>");
foreach (var item in result)
{
sb.Append(string.Format("<li><a href='/Home/FileDownLoad/{0}'>{1}</a></li>", item.FilePath, item.FileName)); }
sb.Append("</ul>");
ViewBag.FileList = sb.ToString();
return View();
} [HttpPost]
public ActionResult FileDownLoad(FormCollection coll)
{
string[] s = { "", "" };
foreach (var item in s)
{
byte[] result = client.FileDownLoad(item);
//输出流的编码
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
//输出类型为流文件
Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + "新建文本文档.txt");
Response.BinaryWrite(result);
Response.Flush();
Response.End(); }
return new EmptyResult();
}
写得不好,可能有错请谅解,有错请指出
.NET企业级应用WebService上传下载文件的更多相关文章
- java web service 上传下载文件
1.新建动态web工程youmeFileServer,新建包com,里面新建类FileProgress package com; import java.io.FileInputStream; imp ...
- rz和sz上传下载文件工具lrzsz
######################### rz和sz上传下载文件工具lrzsz ####################################################### ...
- linux上很方便的上传下载文件工具rz和sz
linux上很方便的上传下载文件工具rz和sz(本文适合linux入门的朋友) ##########################################################&l ...
- shell通过ftp实现上传/下载文件
直接代码,shell文件名为testFtptool.sh: #!/bin/bash ########################################################## ...
- SFTP远程连接服务器上传下载文件-qt4.8.0-vs2010编译器-项目实例
本项目仅测试远程连接服务器,支持上传,下载文件,更多功能开发请看API自行开发. 环境:win7系统,Qt4.8.0版本,vs2010编译器 qt4.8.0-vs2010编译器项目实例下载地址:CSD ...
- linux下常用FTP命令 上传下载文件【转】
1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...
- Delphi调用JAVA的WebService上传XML文件(XE10.2+WIN764)
相关资料:1.http://blog.csdn.net/luojianfeng/article/details/512198902.http://blog.csdn.net/avsuper/artic ...
- C#实现http协议支持上传下载文件的GET、POST请求
C#实现http协议支持上传下载文件的GET.POST请求using System; using System.Collections.Generic; using System.Text; usin ...
- HttpClient上传下载文件
HttpClient上传下载文件 java HttpClient Maven依赖 <dependency> <groupId>org.apache.httpcomponents ...
随机推荐
- java第二次实验
1. 本章学习总结 答:学会在java中使用函数调用. 学会在Java程序中使用函数,使程序层次更清晰. 使用StringBuilder代替string拼接,减少内存空间的占用. 使用BigDecim ...
- Java课程设计—学生成绩管理系统(201521123004-林艺如)
1.团队课程设计博客 团队课程设计博客链接 2.个人负责模块或任务说明 ①.Menu Menu.jsp 在页面中给出提示,用HTML的 MenuTeacher.jsp 利用Menu.jsp进行具体化完 ...
- 201521123062《Java程序设计》第10周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 1.finally 题目4-2 1.1 截图你的提交结果(出现学号) 1.2 4-2中fin ...
- Java多线程高并发学习笔记(三)——深入理解线程池
线程池最核心的一个类:ThreadPoolExecutor. 看一下该类的构造器: public ThreadPoolExecutor(int paramInt1, int paramInt2, lo ...
- 01_ExtJS_HelloWorld
1, 什么是Ext? Ext是一个Ajax框架,用于在客户端创建丰富多彩的web应用程序界面,是在Yahoo! UI的基础上发展而来的.官方网址:http://www.extjs.com/ 现在改为: ...
- Memcached-高性能的分布式内存缓存服务器
Memcached是高性能的分布式内存缓存服务器,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库检索的结果等, 由国外社区网站 LiveJou ...
- 【漏洞公告】CVE-2017-12615/CVE-2017-12616:Tomcat信息泄漏和远程代码执行漏洞
2017年9月19日,Apache Tomcat官方确认并修复了两个高危漏洞,漏洞CVE编号:CVE-2017-12615和CVE-2017-12616,该漏洞受影响版本为7.0-7.80之间,在一定 ...
- MyBatis学习(一)简介及入门案例
1.什么是MyBatis? MyBatis是一个支持普通SQL查询,存储过程,和高级映射的优秀持久层框架.MyBatis去掉了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBati ...
- 最小覆盖_KEY
最小覆盖(cover)..线段树 [题目描述] 给定 N 个区间[Li,Ri],需要你按照顺序选出一个区间序列使得[1,M]完全被覆盖.并且在选出来的序列中,某个区间[a,b]之前必须保证[1,a]都 ...
- 详解AngularJS中的依赖注入
点击查看AngularJS系列目录 依赖注入 一般来说,一个对象只能通过三种方法来得到它的依赖项目: 我们可以在对象内部创建依赖项目 我们可以将依赖作为一个全局变量来进行查找或引用 我们可以将依赖传递 ...