.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. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 答:①String类是final类,不能定义String的子类. ② instanceof ...
- Git与码云(Git@OSC)入门-如何在实验室和宿舍同步你的代码(2)
4. 处理冲突 4.1 向远程仓库push时无法提交成功,提示在push前应该先pull 如图所示: 有可能是因为远程仓库的版本与本地仓库的版本不一致,所以应先git pull将远程仓库的内容合并到本 ...
- 201521044091 java 第十周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. Runnable不是线程,Thread才是,必须将实现Runnable接口的类的对象放入Thread中才能在 ...
- day2_操作系统
一.为什么要有操作系统 因为计算机系统主要是由一个或者多个处理器,主存,硬盘,键盘,鼠标,显示器,打印机,网络接口及其他输入输出设备组成.现代计算机系统复杂 每位计算机程序员不可能全部的掌 ...
- webservice03#schema#元素属性定义
工具软件XMLSpy 2010 破解版,是非常好的写XMl的工具软件. 1,Schema的好处: Schema出现的目的是通过一个更加合理的方式来编写xml的限制文件(基于xml语法的方式): Sch ...
- 高德地图markers生成和点击
因为自己平时上班也是比较忙,遇到什么写什么,希望能给现在的你一些帮助,都是自己在工作中遇到的问题,给自己一个提醒,也是分享 相信很多人在做高德地图开发的时候,对于新手,官方的demo解读单个marke ...
- Node.js博客搭建
Node.js 博客搭建 一. 学习需求 Node 的安装运行 会安装node,搭建node环境 会运行node. 基础模块的使用 Buffer:二进制数据处理模块 Event:事件模块 fs:文件系 ...
- Jquery一些常用的方法
整理以前的笔记,在学习JavaScript时候,经常会用到一些方法,但是有时忘掉了具体用法,因此记下.方便以后查阅. 这篇博文先说明这些方法的用途: removeClass().remove().cs ...
- XCode消除警告、错误
1.集成支付宝SDK后,报一堆warning: (arm64) /Users/scmbuild/workspace/standard-pay/.....警告 解决方法: 1) Go to Build ...
- redhat下yum命令安装(替换为centos yum命令)
redhat默认自带的yum源需要注册,才能更新,报错:This system is not registered to Red Hat Subscription Management. You ca ...