2011-02-12 57 views
2

因此,我使用ASP.NET 2.0并尝试使用简单的窗体将文件上载到Web服务。使用表单输入将文件上传到Web服务时出现问题

我有动作attrib设置为我的web服务的网址。但是,在Firefox中,我看不到它根本就无法拨打该服务。
注意:我可以将int下面的“Action”值放到浏览器中,减去Web方法的名称,并获取显示可用Web方法的页面,因此我相信“Action”属性的URL是正确的。

<form id="fileUpload" action="http://localhost/AcmeABC/services/FileUploadService.asmx/ImportRates" method="post" enctype="multipart/form-data"> 
<input type="text" id="fileName" name="fileName" /> 
<asp:FileUpload runat="server" id="fileArray"/> 
<input type="submit" value="Submit" /> 

[WebService(Namespace = "http://www.abc.com/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[ToolboxItem(false)] 
[ScriptService] 
public class FileUploadService : System.Web.Services.WebService { 

    [WebMethod] 
    public void ImportRates(string fileName, byte[] fileArray) { 
     try { 
      MemoryStream memoryStream = new MemoryStream(fileArray); 
     } 
     catch (Exception ex) { 
      string error = string.Format("Error thrown for file {0} with {1} error.", fileName, ex); 
     } 
    } 

如何查看是怎么回事,因为我没有看到任何呼叫进行。

我也认为这可能不是最好的方法。我对整个网络开发领域都很陌生,所以我正试图寻找更好的方法来处理问题。请建议可能建议将文件上传到网络方法的其他方法。

谢谢,

回答

2

问题在于读取二进制数据作为参数输入。你需要被读取的HttpContext输入流

数据修改您的Web服务方法如下

[WebMethod] 
public void ImportRates() 
{ 
    try 
    { 


     //HTTP Context to get access to the submitted data 
     HttpContext postedContext = HttpContext.Current; 
     //File Collection that was submitted with posted data 
     HttpFileCollection files = postedContext.Request.Files; 
     //Make sure a file was posted 
     string fileName =files[0].FileName; 

     MemoryStream memoryStream = new MemoryStream(files[0].InputStream); 


    } 
    catch (Exception ex1) 
    { 
     string error = string.Format("Error thrown for file {0} with {1} error.", fileName, ex); 

    } 
} 
0

你能告诉我们在ImportRates web方法中的代码吗?它看起来像网络方法here

除了Web服务文件上传以外,另一个选择是ASHX handler。您可以将ashx处理程序视为不带视图的asp.net代码隐藏文件。一般来说,ashx处理程序被认为比web服务更轻量级。

+0

我会更新我的问题与我的Web方法的当前状态。它与链接相似。但我只是想读取文件并在其中断点。然而,我从来没有达到这个突破点,萤火虫也没有发现任何呼叫服务。 – pghtech 2011-02-12 15:31:31

+0

我至少会认为它会报告找不到的服务。 – pghtech 2011-02-12 15:35:48

0

我只注意到动作url中没有指定端口。这可能是完美的,取决于你的开发环境,但值得一提的是。

相关问题