前兩個函數運行良好,但第三個函數有一些錯誤不清楚。這裏是所有的代碼,你可以幫助我如何完成最後一個功能。c#web應用程序調用web服務來寫服務器上的文件
代碼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()" /> <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>
它似乎是寫入權限問題。請授予寫入權限並嘗試。 –