如何将用户编写的文本上传到 asp.net 通用处理程序?文字很长。
问问题
553 次
2 回答
1
查看您评论中的代码(感谢分享),似乎包含您的文本的参数data
在您的 JavaScript 中被调用,并且您正在处理程序中寻找file
。
尝试:context.Request.Form["data"]
在您的处理程序中。
于 2013-01-01T20:44:45.960 回答
1
试试这样的 jQuery 代码:
jQuery(document).ready(function($){
$('#button-id-to-submit-info-to-the-handler').on('click', function(ev) {
ev.preventDefault();
//Wrap your msg in some fashion
//in case you want to end other things
//to your handler in the future
var $xml = $('<root />')
.append($('<msg />', {
text: escape($('#id-of-your-textarea-that-has-the-text').val())
}
));
$.ajax({
type:'POST',
url:'/path/to-your/handler.ashx',
data: $('<nothing />').append($xml).html(),
success: function(data) {
$('body').prepend($('<div />', { text: $(data).find('responsetext').text() }));
}
});
});
});
在你的处理程序中:
public class YourHandler : IHttpHandler
{
public void ProcessRequest(HttpContext ctx)
{
//Response with XML
//Build a response template
ctx.Response.ContentType = "text/xml";
String rspBody = @"<?xml version=\""1.0\"" encoding=\""utf-8\"" standalone=\""yes\""?>
<root>
<responsetext>{0}</responsetext>
</root>";
//Get the xml document created via jquery
//and load it into an XmlDocument
XmlDocument xDoc = new XmlDocument();
using (System.IO.StreamReader sr = new StreamReader(ctx.Request.InputStream))
{
String xml = sr.ReadToEnd();
xDoc.LoadXml(xml);
}
//Find your <msg> node and decode the text
XmlNode msg = xDoc.DocumentElement.SelectSingleNode("/msg");
String msgText = HttpUtility.UrlDecode(msg.InnerXml);
if (!String.IsNullOrEmpty(msgText))
{
//Success!!
//Do whatever you plan on doing with this
//and send a success response back
ctx.Response.Write(String.Format(rspBody, "SUCCESS"));
}
else
{
ctx.Response.Write(String.Format(rspBody, "FAIL: msgText was Empty!"));
}
}
}
于 2013-01-01T20:46:14.230 回答