c #Web应用程序调用Web服务在服务器上写入文件

时间:2012-09-04 08:58:49

标签: javascript c# web-applications file-io

前两个功能运行良好但第三个功能有些错误不清楚。以下是所有代码,您能否帮我完成最后一项功能。

WebService1.asmx中的

代码

using System.ComponentModel;
    using System.Web.Services;
    using System.Text;
    using System.IO;

namespace WebApplication1
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorldFun1()
        {
            return "Hello World";
        }
        [WebMethod]
        public string HelloWorldFun2(string str)
        {
            return "Hello World,"+str;
        }
        [WebMethod]
        public string Write_to_File(string str)
        {
            StreamWriter _testData = new StreamWriter(Server.MapPath("~/output.txt"), true);
            _testData.WriteLine(str); // Write the file.
            _testData.Flush();
            _testData.Close(); // Close the instance of StreamWriter.
            _testData.Dispose(); // Dispose from memory. 
            return str;
        }

    }
}

Default.aspx中的代码

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>WebService</title>
    <script type="text/javascript" language="javascript">
        function func1()
        {        
        WebApplication1.WebService1.HelloWorldFun1(onSuccess,onFail,'Span1');
        }
        function func2()
        {
        var txt=document.getElementById('Text1').value;
        WebApplication1.WebService1.HelloWorldFun2(txt,onSuccess,onFail,'Span2');
        }
        function write()
        {
        var txt = document.getElementById("Span1").innerHTML;
        WebApplication1.WebService1.Write_to_File("kkkkkkkkkk",onSuccess,onFail,'Span3');
        }
        function onSuccess(value,context)
        {
        document.getElementById(context).innerHTML=value;
        }
        function onFail(value)
        {
        alert(value);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    <Services>
        <asp:ServiceReference Path="~/WebService1.asmx"  />
    </Services>
    </asp:ScriptManager>
    <input id="Button1" type="button" value="button"  onclick="func1()" />&nbsp;&nbsp;&nbsp;&nbsp;<span id="Span1"></span>
    <hr />
    <input id="Text1" type="text" /><input id="Button2" type="button" value="button"  onclick="func2()" /><span id="Span2"></span>
    </form> 
    <input id = "WTF" type = "button" value = "write" onclick ="write()"/><span id="Span3"></span>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

1)错误是什么? (运行时异常\堆栈跟踪或其编译错误?)

2)_testData.Close(); //关闭StreamWriter的实例。    _testData.Dispose(); //从记忆中处理。

调用Close()就足够了(这与Dispose相似,所以你要处理两次......不是错误,但仍然很高兴知道。)

答案 1 :(得分:0)

Web服务似乎没问题,并且可以正常工作(否则你会得到一个例外)。 Default.aspx似乎有问题。

我建议使用JQuery来调用Web服务。

答案 2 :(得分:0)

我知道原因。应该有一些名为“write()”的已定义函数。我只是将名称“write()”更改为“write_to()”然后它运行良好。感谢您的回答,Vitali Kaspler。