osh*_*nen 4 c# ajax jquery web-services asmx
更新的问题:
我正在尝试创建一个将主题,内容和文件传递给Web服务的表单.这就是我到目前为止所想的,并且想知道是否有人可以告诉我我是否朝着正确的方向前进,以及如何在asmx文件的注释中突出显示的位
HTML:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form action="files.asmx/CaptureFile" enctype="multipart/form-data" method="post">
<input type="text" name="subject" /><br />
<input type="text" name="content" /><br />
<input type="file" name="filedata" /><br />
<input type="submit" value="Upload" />
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
WebService:
<%@ WebService Language="C#" Class="Files" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Script;
using System.Web.Script.Services;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
[ScriptService]
public class Files : WebService {
SqlConnection connection;
SqlCommand command;
SqlDataReader reader;
int intAffectedRows;
[WebMethod()]
public int CaptureFile(string subject, string content, byte[] filedata)
{
// somehow reconstruct the filedata to an actual file saved on the server
// save subject, content, and filename to database
using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
{
using (command = new SqlCommand("query to save subject content and filename_only to database", connection))
{
command.Parameters.Add("@subject", SqlDbType.VarChar, 255).Value = subject;
command.Parameters.Add("@content", SqlDbType.VarChar, 255).Value = content;
command.Parameters.Add("@filedata", SqlDbType.VarChar, 255).Value = filedata; // need to save filename here, not file binary data
connection.Open();
intAffectedRows = command.ExecuteNonQuery();
connection.Close();
}
}
return intAffectedRows;
}
}
Run Code Online (Sandbox Code Playgroud)
原始问题:
我理解如何将标准文本发送到Web服务器,处理它,然后发回一些东西,即
[WebMethod()]
public List<Notification> GetNotification(int id)
{
// do processing here
// return something back
return "Notification text";
}
Run Code Online (Sandbox Code Playgroud)
我的ajax看起来像这样:
$.ajax({
type: 'POST',
url: '/webservices/notifications.asmx/GetNotification',
data: '{id: ' + number + '}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
Run Code Online (Sandbox Code Playgroud)
我如何发送文件?该文件将是.pdf或.doc文件,因此我可以将其与一些文本一起保存到服务器.所以,我想要一个主题的文本框,并选择文件按钮/文本框来选择文件,并提交一个提交按钮.当填写2个文本框并单击提交按钮时,它应该将主题和文件发送到Web服务,即web服务将主题和文件位置保存到数据库,并将实际文件保存到服务器.
此外,我正在内部网环境中开发,IE完全信任本地Intranet.
我如何发送文件?
您不能使用AJAX发送文件只是因为使用javascript您无法访问客户端计算机上的文件系统,因此您无法获取文件内容.如果要将文件发送到Web服务器,可以使用<form>带文件输入,并将enctype="multipart/form-data"其发布到服务器端脚本.然后,该脚本可以调用Web服务并将文件内容作为字节数组传输.
| 归档时间: |
|
| 查看次数: |
7129 次 |
| 最近记录: |