C# 通过WebService方式 IIS发布网站 上传文件到服务器
应用场景:要将本地的文件 上传到服务器的虚拟机上
网络环境:公司局域网(如下图中第二种)
开发环境:VS2010
服务器环境:WinServer2008 虚拟机环境:WinServer2008
我的程序结构目录
AppSrvice 是服务文件 将来发布了以后要放到服务器上, WindowFormsAppp 是Winform程序
第一步:
创建一个新的: Windows窗体应用程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
//using System.Threading;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
using System.Web.Services;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//localhost.WebService1 client = new localhost.WebService1();
WindowsFormsApp.ServiceReference1.Service1SoapClient client = new WindowsFormsApp.ServiceReference1.Service1SoapClient();
//WindowsFormsApp.ServiceReference1.WebService1SoapClient client = new WindowsFormsApp.ServiceReference1.WebService1SoapClient();
//上传服务器后的文件名 一般不修改文件名称
int start = textBox1.Text.LastIndexOf("\");
int length = textBox1.Text.Length;
string serverfile = textBox1.Text.Substring(start + 1, length - textBox1.Text.LastIndexOf("."))
+ DateTime.Now.ToString("-yyyy-mm-dd-hh-mm-ss")
+ textBox1.Text.Substring(textBox1.Text.LastIndexOf("."), textBox1.Text.Length - textBox1.Text.LastIndexOf("."));
client.CreateFile(serverfile);
//要上传文件的路径
string sourceFile = textBox1.Text;
string md5 = GetMD5(sourceFile);
FileStream fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
int size = (int)fs.Length;
int bufferSize = 1024 * 512;
int count = (int)Math.Ceiling((double)size / (double)bufferSize);
for (int i = 0; i < count; i++)
{
int readSize = bufferSize;
if (i == count - 1)
readSize = size - bufferSize * i;
byte[] buffer = new byte[readSize];
fs.Read(buffer, 0, readSize);
client.Append(serverfile, buffer);
}
bool isVerify = client.Verify(serverfile, md5);
if (isVerify)
MessageBox.Show("上传成功");
else
MessageBox.Show("上传失败");
}
private string GetMD5(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
MD5CryptoServiceProvider p = new MD5CryptoServiceProvider();
byte[] md5buffer = p.ComputeHash(fs);
fs.Close();
string md5Str = "";
List strList = new List();
for (int i = 0; i < md5buffer.Length; i++)
{
md5Str += md5buffer[i].ToString("x2");
}
return md5Str;
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
//openDialog.Filter = "视频文件(*.avi,*.wmv,*.mp4)|*.avi;*.wmv;*.mp4";
if (openDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openDialog.FileName;
}
}
}
}
第二步:创建WebService
关键代码如下:
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;
using System.Security.Cryptography;
using System.Collections.Generic;
{
///
/// Service1 的摘要说明
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
public string HelloWorld()
{
return "Hello World";
}
public bool CreateFile(string fileName)
{
bool isCreate = true;
try
{
fileName = Path.Combine(Server.MapPath(""), Path.GetFileName(fileName));
//首先设置上传服务器文件的路径 然后发布web服务 发布的时候要自己建一个自己知道的文件夹 "C:\NMGIS_Video" "C:\NMGIS_Video" fileName = Path.Combine(Server.MapPath("") + @"\Video" + Path.GetFileName(fileName));
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
fs.Close();
}
catch
{
isCreate = false;
}
return isCreate;
}
[WebMethod]
public bool Append(string fileName, byte[] buffer)
{
bool isAppend = true;
try
{
//fileName = Path.Combine(@"C:\NMGIS_Video" + Path.GetFileName(fileName));
fs.Seek(0, SeekOrigin.End);
fs.Write(buffer, 0, buffer.Length);
fs.Close();
}
catch
{
isAppend = false;
}
return isAppend;
}
[WebMethod]
public bool Verify(string fileName, string md5)
{
bool isVerify = true;
try
{
// fileName = Server.MapPath("D:\MesWebCR\picture") + Path.GetFileName(fileName);
MD5CryptoServiceProvider p = new MD5CryptoServiceProvider();
byte[] md5buffer = p.ComputeHash(fs);
fs.Close();
string md5Str = "";
List strList = new List();
for (int i = 0; i < md5buffer.Length; i++)
{
md5Str += md5buffer[i].ToString("x2");
}
if (md5 != md5Str)
isVerify = false;
}
catch
{
isVerify = false;
}
return isVerify;
}
}
选中服务项目,右键 发布
发布方法选择:文件系统
目标位置:是选择你发布后生成文件的位置自己随便找个地方即可
然后点击 发布
第四步:拷贝文件到服务器
将刚才发布好的文件拷贝到你要上传到的服务器的虚拟机的指定目录下
第五步:在虚拟机上发布网站
打开虚拟机的IIS 发布一个网站文件路径指向你刚才拷贝到虚拟机上的文件目录
IP地址就是当前虚拟机的IP 要设置为固定的IP
端口一定注意 不要与当前正在使用的端口冲突建议更改一个
然后确定 发布网站
选中刚才发布的网站 ,右边滚动条向下,选择 默认文档并双击
双击打开后右边点击添加按钮 ,当刚才复制到虚拟机当中的 .asmx 文件名添加到里边点确定
网站右键,管理网站,浏览 查看发不好的网站是否可以访问
我这里浏览是可以访问的:如下图
第六步:设置虚拟机网络环境
虚拟机》编辑 或者 开始菜单中 找到 Virtral Network Editor
打开虚拟网络编辑器
Nat 设置里边 映射两个端口 TCP、UDP类型各一个, 然后点击确定
宿主机的8070 端口映射到虚拟机的“192.168.16.135”的8070端口了,因为web服务自动开放的端口是8070,所以,只要我们访问 “http://192.168.1.54:8070”,就可以访问到虚拟机的8070端口,也就是web服务了(这里宿主机的端口可以改成其他端口,无需跟虚拟机端口一致,我是为了省事都写成了8070)
然后点击应用 确定 。。。这里设置好了以后就可以通过访问虚拟机的宿主机IP访问到你虚拟机上的服务
此时就可以通过其他任意机器访问你的.asmx页面注意你的端口一定要正确正如上面所说:这里直接通过访问宿主机的Ip就可以
第七步:为客户端添加服务器引用
项目右键添加服务引用
添加服务地址 点击 前往 ,如果正确的话 会在下面显示出你的服务页面然后点击确定
第八步:测试
运行客户端测试
这里显示成功了 那么我们去虚拟机的目录下看一看到底有没有
C# 通过WebService方式 IIS发布网站 上传文件到服务器的更多相关文章
- C# 通过WebService方式 IIS发布网站 上传文件到服务器[转]
http://blog.sina.com.cn/s/blog_517cae3c0102v0y7.html 应用场景:要将本地的文件 上传到服务器的虚拟机上 网络环境:公司局域网(如下图中第二种) 开发 ...
- WPF上传文件到服务器
利用WebClient 上传文件到服务器 创建一个空网站,创建一个UploadFile.aspx项, 服务器报500错误:检查文件保存路径是否存在,检查文件大小限制 protected void Pa ...
- Android上传文件至服务器(上)
每一次都不能上首页,真悲催..管理员让我上一次首页? 很多时候我更愿意一个人写代码,与其在垃圾代码上改改改,我更愿意直接重构. 整洁的代码简单直接.整洁的代码如同优美的散文.整洁的代码从不隐藏设计者的 ...
- android上传文件到服务器
package com.spring.sky.image.upload.network; import java.io.DataOutputStream; import java.io.File; i ...
- android -上传文件到服务器
android上传文件到服务器 重点:最好是设置好content-type这些参数的配置! package com.spring.sky.image.upload.network; ...
- C 上传文件到服务器(含接收端源码)
本文demo下载地址:http://www.wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=1067 实例向大家展示了如何用Visua ...
- J2EE:Servlet上传文件到服务器,并相应显示
Servlet 可以与HTML一起使用来允许用户上传文件到服务器 编辑上传文件的页面upload.html 注意事项:上传方式使用POST不能使用GET(GET不能上传文件) 表单 enctype 属 ...
- Android端通过HttpURLConnection上传文件到服务器
Android端通过HttpURLConnection上传文件到服务器 一:实现原理 最近在做Android客户端的应用开发,涉及到要把图片上传到后台服务器中,自己选择了做Spring3 MVC HT ...
- joomla安装插件报错:上传文件到服务器发生了一个错误。 过小的PHP文件上传尺寸
在安装joomla的AKeeba插件的时候报错如下:上传文件到服务器发生了一个错误. 过小的PHP文件上传尺寸.解决方法是修改php.ini文件,打开文件后搜索upload_max_filesize! ...
随机推荐
- 网站标题ico那些事
浏览器打开一个网页都会有一个标题,用来显示当前页面的相关内容,如网站名称或者一篇文章的大标题,而定义它应该显示啥的话完全由HTML中title标签的内容决定. 如我们的大博客园:
- Leetcode 345 Reverse Vowels of a String 字符串处理
题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. class Solution { public: bool isVowel ...
- Docker实践(1)—入门
tutorial centos6.5环境. # yum install docker-io -y 会依赖安装libcgroup,lxc,lxc-libs 启动docker # service dock ...
- (ETW) Event Tracing for Windows 入门 (含pdf下载)
内容提纲 • ETW 介绍 • ETW 使用 • ETW 监控本机Demo • ETW 监控远程机器的思路 • 底层类库:EventSource 介绍 • 底层类库:TraceEvent 介绍 ETW ...
- OpenSSL命令---pkcs7
用途: 用于处理DER或者PEM格式的pkcs#7文件. 用法: openssl pkcs7 [-inform PEM|DER] [-outform PEM|DER] [-in filename] [ ...
- web项目总结
web项目 Webroot下面的index.jsp页面的内容: <%@ page language="java" pageEncoding="UTF-8" ...
- 【linux】——FreeBSD 建立 SSH 连接慢的解决方法
一般在编写 linux 程序的时候,会使用 SecureCRT 或者 xshell 等工具远程登录到 linux 服务器上.最近发现在建立 SSH 连接的时候,非常慢,但是建立连接成功之后可以正常使用 ...
- 修改windows系統下xampp中apache端口被其他程式占用的問題
windows 7安裝後啟動xampp, 提示port 443 被其他程式占用. 網上查找解決方案: http://stackoverflow.com/questions/21182512/how-t ...
- 本博客停止更新,移至purecpp.org
声明:本博客停止更新,迁移至C++开源社区purecpp.org, 欢迎持续关注!
- songtaste网站歌曲真实URL获取
个人挺喜欢songtaste网站的歌曲的,下载方法也层出不穷,可是作为程序员如果不知其中原理的方法真是羞愧.首先简单点的方法当然有google插件这样的嗅探器了,不过这种工具的原理还不是很了解.今天先 ...