在建立好一个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();
}

写得不好,可能有错请谅解,有错请指出

最新文章

  1. github安装&amp;初探
  2. linux驱动之LCD
  3. webpy 访问局域网共享资源
  4. 逗号分隔的字符串转换为行数据(collection)(续)
  5. POJ 2828 线段树(想法)
  6. SNF开发平台WinForm之四-开发-主细表管理页面-SNF快速开发平台3.3-Spring.Net.Framework
  7. Effective java笔记5--通用程序设计
  8. Java SE (5)之 线程使用
  9. AndroidStudio SVN检出
  10. Skype发布视频API
  11. iosAPP打包上架xcode中Archive提交成功以后,不提示构建版本问题
  12. SDWebImage源码解读之分类
  13. mysql 导出每张表中的100条数据..............
  14. 第七章 mysql 事务索引以及触发器,视图等等,很重要又难一点点的部分
  15. 论文笔记(一)---翻译 Rich feature hierarchies for accurate object detection and semantic segmentation
  16. node.js中express的Router路由的使用
  17. [iOS]Xcode+GitHub远程代码托管(GIT, SVN)
  18. Part 2 - Fundamentals(4-10)
  19. BZOJ 4326 NOIP2015 运输计划 (二分+树上差分)
  20. php自带的filter过滤函数

热门文章

  1. 201521123093 java 第四周学习总结
  2. Java 课程设计 &quot;Give it up&quot;小游戏(团队)
  3. 201521123017 《Java程序设计》第14周学习总结
  4. tsst
  5. Day-17: 网络编程
  6. temp--达州银行
  7. birt 集成到现有的web应用中
  8. u盘分区装机版
  9. MongoDB 所支持的数据类型 创建和删除集合 创建和删除数据库
  10. 11 Linear Models for Classification