http://blog.sina.com.cn/s/blog_517cae3c0102v0y7.html

应用场景:要将本地的文件 上传到服务器的虚拟机上

网络环境:公司局域网(如下图中第二种)

开发环境: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;
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;
namespace WebService1
{
    ///
    /// 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
    {
         
       
[WebMethod]
       
public string HelloWorld()
       
{
           
return "Hello World";
       
}
       
[WebMethod]
       
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));  

               
fileName = Path.Combine(Server.MapPath(""),
Path.GetFileName(fileName));
               
FileStream fs = new FileStream(fileName, FileMode.Open,
FileAccess.ReadWrite, FileShare.ReadWrite);
               
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 = Path.Combine(Server.MapPath(""),
Path.GetFileName(fileName));
               
// fileName =
Server.MapPath("D:\MesWebCR\picture")  +
Path.GetFileName(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");
               
}
               
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就可以

第七步:为客户端添加服务器引用

项目右键
添加服务引用

添加服务地址 点击 前往 ,如果正确的话 会在下面显示出你的服务页面
然后点击确定

第八步:测试

运行客户端测试

这里显示成功了 
那么我们去虚拟机的目录下看一看到底有没有

这里有两个文件 我测试了两次 , 
文件名称我在程序当中追加了当前时间的格式化字符串,文件大小也是对的。

免积分 源码下载
地址:http://download.csdn.net/detail/u010011052/7213805

C# 通过WebService方式 IIS发布网站 上传文件到服务器[转]的更多相关文章

  1. C# 通过WebService方式 IIS发布网站 上传文件到服务器

    应用场景:要将本地的文件 上传到服务器的虚拟机上 网络环境:公司局域网(如下图中第二种) 开发环境:VS2010 服务器环境:WinServer2008    虚拟机环境:WinServer2008 ...

  2. WPF上传文件到服务器

    利用WebClient 上传文件到服务器 创建一个空网站,创建一个UploadFile.aspx项, 服务器报500错误:检查文件保存路径是否存在,检查文件大小限制 protected void Pa ...

  3. Android上传文件至服务器(上)

    每一次都不能上首页,真悲催..管理员让我上一次首页? 很多时候我更愿意一个人写代码,与其在垃圾代码上改改改,我更愿意直接重构. 整洁的代码简单直接.整洁的代码如同优美的散文.整洁的代码从不隐藏设计者的 ...

  4. android上传文件到服务器

    package com.spring.sky.image.upload.network; import java.io.DataOutputStream; import java.io.File; i ...

  5. android -上传文件到服务器

    android上传文件到服务器       重点:最好是设置好content-type这些参数的配置!     package com.spring.sky.image.upload.network; ...

  6. C 上传文件到服务器(含接收端源码)

    本文demo下载地址:http://www.wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=1067 实例向大家展示了如何用Visua ...

  7. J2EE:Servlet上传文件到服务器,并相应显示

    Servlet 可以与HTML一起使用来允许用户上传文件到服务器 编辑上传文件的页面upload.html 注意事项:上传方式使用POST不能使用GET(GET不能上传文件) 表单 enctype 属 ...

  8. Android端通过HttpURLConnection上传文件到服务器

    Android端通过HttpURLConnection上传文件到服务器 一:实现原理 最近在做Android客户端的应用开发,涉及到要把图片上传到后台服务器中,自己选择了做Spring3 MVC HT ...

  9. joomla安装插件报错:上传文件到服务器发生了一个错误。 过小的PHP文件上传尺寸

    在安装joomla的AKeeba插件的时候报错如下:上传文件到服务器发生了一个错误. 过小的PHP文件上传尺寸.解决方法是修改php.ini文件,打开文件后搜索upload_max_filesize! ...

随机推荐

  1. launchMode

    launchMode在多个Activity跳转的过程中扮演着重要的角色,它可以决定是否生成新的Activity实例,是否重用已存在的Activity实例,是否和其他Activity实例公用一个task ...

  2. Selenium WebDriver-操作复选框

    #encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...

  3. 03-python进阶-爬虫入门-正则

    [urllib and urllib2] 这是两个python的网络模块 内置的 提供很好的网络访问的功能. #!coding:utf-8 import urllib2 res = urllib2.u ...

  4. html 标签附加文本属性

    <!DOCTYPE html> <html> <head> <script> function showDetails(animal) { var an ...

  5. Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined)

    我真的是太菜了 A. Perfect Squares time limit per test 1 second memory limit per test 256 megabytes input st ...

  6. jQuery 样式操作、文档操作、属性操作的方法总结

    文档操作: addClass()             向匹配的元素添加指定的类名.after()                    在匹配的元素之后插入内容.append()         ...

  7. 【bzoj4016】[FJOI2014]最短路径树问题 堆优化Dijkstra+DFS树+树的点分治

    题目描述 给一个包含n个点,m条边的无向连通图.从顶点1出发,往其余所有点分别走一次并返回. 往某一个点走时,选择总长度最短的路径走.若有多条长度最短的路径,则选择经过的顶点序列字典序最小的那条路径( ...

  8. CS231n笔记 Lecture 5 Convolutional Neural Networks

    一些ConvNets的应用 Face recognition 输入人脸,推测是谁 Video classfication Recognition 识别身体的部位, 医学图像, 星空, 标志牌, 鲸.. ...

  9. 第一个 XMLHttpRequest 例子(API)

    [API] https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest [替代方案] 如果不想自己敲代码,可以直接访问以下URL测试 ...

  10. 写论文,关于word的技巧

    当段落的行间距为固定值的时候,图片会出现显示不全的情况,将行间距先改了再插入图片就没问题了. 从引用那边自动添加目录. ctrl+H打开可以使用通配符,改变字母或者数字的样式等