zoukankan      html  css  js  c++  java
  • 写了一个Web服务上传文件的例子,大家有空随便看看

    写了一个Web服务上传文件的例子,大家有空随便看看

    刚才写了个例子,试试用WebService上传文件,还可以,现在把例子贴出来给大家看看:

    服务器代码:

    服务器代码简单的实现了一个UploadFile方法,其中接收的是byte[]数组,我没有试过其他的类型,估计FileStream过不来,还是简单类型来的方便,第二个参数是要保存的相对路径,方法执行完成后返回在服务器上的物理文件路径.写的简单了点,见谅.明白意思就行了.

    using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.IO; /// /// UploadService 的摘要说明 /// [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class UploadService : System.Web.Services.WebService { public UploadService() { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public string UploadFile(byte[] file,string path) { try { string phyPath = Server.MapPath(path); FileStream fs = new FileStream(phyPath,FileMode.Create, FileAccess.Write); fs.Write(file, 0, file.Length); fs.Close(); return phyPath; } catch (Exception ex) { throw ex; } } }

    客户端调用代码:(要先添加Web引用,指向Asmx文件,如 http://localhost:2339/TestSite/UploadService.asmx )

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace ServiceTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { openFileDialog1.ShowDialog(this); textBox1.Text = openFileDialog1.FileName; } private void button1_Click(object sender, EventArgs e) { FileStream fs = File.OpenRead(textBox1.Text); byte[] file = new byte[fs.Length]; fs.Read(file, 0, file.Length); localhost.UploadService us = new ServiceTest.localhost.UploadService(); textBox2.Text = us.UploadFile(file, Path.GetFileName(textBox1.Text)); } } }

    这里并没有一些特殊情况做过多判断,在Web项目中增加了一个WebService,然后从本机上写了一个WinForm项目来调用,要注意的是上传大文件的时候,要把Web.config中改一下,具体做法如下:(注意 httpRuntime 那一项)

    <system.web> <httpRuntime maxRequestLength="2097151"/> <!-- 设置 compilation debug="true" 将调试符号插入 已编译的页面中。但由于这会 影响性能,因此只在开发过程中将此值 设置为 true。 --> <compilation debug="true"> <assemblies> <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation> <!-- 通过 <authentication> 节可以配置 ASP.NET 使用的 安全身份验证模式, 以标识传入的用户。 --> <authentication mode="Windows"/> <!-- 如果在执行请求的过程中出现未处理的错误, 则通过 <customErrors> 节可以配置相应的处理步骤。具体说来, 开发人员通过该节可以配置 要显示的 html 错误页 以代替错误堆栈跟踪。 <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors> --> </system.web>


    引用地址:http://www.openzone.cn/blogs/opennet/archive/2006/07/23/34.aspx
  • 相关阅读:
    JDK8中的 Lambda 表达式
    IDEA导入新项目jar包以及项目依赖tomcat设置
    idea导入项目,类为灰色,左下角有个红圈
    mysql服务忽然挂了,出现错误信息: Can’t connect to MySQL server on ‘localhost’ (10061)
    mysql、oracle、sql server连接信息
    mybatis中select * 中有字段,自己在起一个别名,然后实体类会使用哪个?
    pymongo的操作
    mongodb备份恢复
    mongodb建立索引
    mongodb聚合命令
  • 原文地址:https://www.cnblogs.com/hhq80/p/688397.html
Copyright © 2011-2022 走看看