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

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

开发环境: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就可以

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

项目右键添加服务引用

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

第八步:测试

运行客户端测试

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

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

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

    http://blog.sina.com.cn/s/blog_517cae3c0102v0y7.html 应用场景:要将本地的文件 上传到服务器的虚拟机上 网络环境:公司局域网(如下图中第二种) 开发 ...

  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. iOS开发——高级技术&调用地图功能的实现

    调用地图功能的实现 一:苹果自带地图 学习如逆水行舟,不进则退.古人告诉我们要不断的反思和总结,日思则日精,月思则月精,年思则年精.只有不断的尝试和总结,才能让我们的工作和生活更加 轻松愉快和美好.连 ...

  2. Leetcode 290 Word Pattern STL

    Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...

  3. FindFriendsServer服务搭建

    本文介绍如何搭建FindFriendsServer(https://github.com/hnrainll/FindFriendsServer)所需的环境. 环境需要: Windows+Apache+ ...

  4. android ormlite queryBuilder.where() 多条件

    QueryBuilder<VideoTagInfo, Integer> queryBuilder = videoTagInfoIntegerDao.queryBuilder();try { ...

  5. 移动APP的IM后台架构浅析

    IM(InstantMessaging 即时通讯)作为一项基础功能,很多APP都有,比如:手机QQ.微信.易信.钉钉.飞信.旺旺.咚咚.陌陌等.而IM如同我们日常生活中的水和电一样,必不可少,也是很多 ...

  6. Python3 字符串

    Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32Type & ...

  7. 在IE7下使用angularjs(转)

    在我的环境中,主要支持的浏览器为:ie7, ie8, ie9, chrome等.在试验angularjs时,发现在ie7环境下运行有问题,于是在网上搜了一下相关的内容,说HTML标签的写法要这样: & ...

  8. 防止WordPress利用xmlrpc.php进行暴力破解以及DDoS

    早在2012 年 12 月 17 日一些采用 PHP 的知名博客程序 WordPress被曝光存在严重的漏洞,该漏洞覆盖WordPress 全部已发布的版本(包括WordPress 3.8.1).该漏 ...

  9. Requirejs加载超时问题的一个解决方法:设置waitSeconds=0

    有时Requirejs会遇到加载js超时问题 除了排查js脚本问题,网络问题以外的一个解决方法是加大Require的等待时间waitSeconds,或者直接设置为0,这个参数的意义是:The numb ...

  10. 【C++沉思录】代理类

    1.考虑下面的场景:设计一个容器,包含一组类型不同但相互关联的对象(比如:Animal,Dog,Cat),对象具备多态行为.2.容器一般只能包含一种类型的对象,使用vector<Animal&g ...