Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > C#.NET > 用WebService上傳文件的兩種方案...
【标  题】:用WebService上傳文件的兩種方案...
【关键字】:WebService,...
【来  源】:http://robin0925.cnblogs.com/archive/2006/05/16/401270.html

用WebService上傳文件的兩種方案...

Your Ad Here
 
第二种方案是我查資料弄明白的,這種方法在文件数据不大的时候可以用。但传输大文件时会遇到超时的错误。C#Hunter告訴我一個更好的方案:
 

方案一:
                          对应的解决方法可以参考:Dime Buffered Upload
                           http://www.codeproject.com/cs/webservices/DimeBufferedUpload.asp 
                           源碼:Download source (WinForms, Web Service) - 545 Kb 
                                    Download demo app (WinForms, Web Service) - 493 Kb



 
方案二 :

Webservice:
        public Service1()
       
{
           InitializeComponent();
        }

                [WebMethod]
         
public string UploadFile(byte[] fs,string FileName)
        
{
            
try
            
{
                
                MemoryStream m 
= new MemoryStream(fs);
                
                
//FileStream f = new FileStream(Server.MapPath(".") + "\\"+ FileName, FileMode.Create);
                FileStream f = new FileStream(@"D:\"+ FileName, FileMode.Create); //這個路徑可以指定的,但是,一定要開放.Net寫的權限
                
                m.WriteTo(f);
                m.Close();
                f.Close();
                f 
= null;
                m 
= null;
                
return "success !";
             }

            
catch(Exception ex)
            
{
                   
return ex.Message;
                }

        }

 

 



調用頁面
     

                try
                
{
                    
                    System.Web.HttpFileCollection oFiles;
                    oFiles 
= System.Web.HttpContext.Current.Request.Files;
                    
string FilePath = oFiles[0].FileName;
                    
string FileName = FilePath.Substring(FilePath.LastIndexOf("\\")+1);

                    WebReference.Service1 s1 
= new com.foxconn.cr.CostomerRisk.WebReference.Service1();
                    
byte[] b = new byte[oFiles[0].ContentLength];
                    System.IO.Stream fs;                
                    
                    fs 
= (System.IO.Stream)oFiles[0].InputStream;
                    fs.Read(b, 
0, oFiles[0].ContentLength);
                    
                    
string tt = s1.UploadFile(b, FileName);
                    fs.Close();
                }

                
catch(Exception ex)
                
{
                    
this.L_msg.Visible=true;
                    
this.L_msg.Text = ex.ToString();
                }
 




从TreeView的节点数据加载到WebService再到Ajax的联想:【上一篇】
在ASP.NET Atlas中调用Web Service——介绍及简单应用:【下一篇】
【相关文章】
  • 从TreeView的节点数据加载到WebService再到Ajax的联想
  • 花瓶中哭泣的百合花.....
  • 第二个页面的错误总结......
  • 网络书签(不断更新........)
  • 按钮可用状态倒计时...
  • 空气清新......
  • 辞职中.......
  • 早起跑步...
  • 关于Hibernate多对多联系...
  • 【C#2.0】点滴(不断补充中......)
  • 【随机文章】
  • 华为笔试题及解答
  • Windows 2000系统编程——进程的创建
  • 实战:WindowsXP系统中装Win2000
  • Netfilter/Iptables的防火墙功能介绍
  • 扩展Web Log日志功能
  • AJAX技术框架及开发工具
  • Solaris下Informix安装配置维护升级基础测试
  • 软件测试工程师笔试试题
  • Char/Binary和Varchar/VarBinary的最大长度
  • Visual Basic 深度下潜之海蛇缠绕
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.