【发布时间】:2012-01-06 11:26:36
【问题描述】:
更新问题:
我正在尝试创建一个将主题、内容和文件传递给 Web 服务的表单。这是我到目前为止所拥有的,想知道是否有人可以告诉我我是否朝着正确的方向前进以及如何执行我在 asmx 文件的 cmets 中突出显示的位
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>
网络服务:
<%@ 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;
}
}
---------
原始问题:
我了解如何将标准文本发送到网络服务器,对其进行处理,然后发回一些内容,即
[WebMethod()]
public List<Notification> GetNotification(int id)
{
// do processing here
// return something back
return "Notification text";
}
我的 ajax 看起来像这样:
$.ajax({
type: 'POST',
url: '/webservices/notifications.asmx/GetNotification',
data: '{id: ' + number + '}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
如何发送文件?该文件将是 .pdf 或 .doc 文件,因此我可以将其与一些文本一起保存到服务器。所以,我想要一个主题的文本框,选择文件按钮/文本框来选择文件,还有一个提交按钮。当两个文本框都填好并点击提交按钮时,它应该将主题和文件发送到webservice,webservice会将主题和文件位置保存到数据库,并将实际文件保存到服务器。
另外,我是在内网环境下开发,IE对本地内网完全信任。
【问题讨论】:
-
传递给方法文件字节数组
-
这样的?
data:{id:1,file:'+filepath+'},然后在服务器上GetFile(int id, byte file)? -
我不知道如何使用 ajax - 我不是网络程序员。但是你需要有网络服务,即
saveFile(byte[] fileData),并在 ajax 中读取文件字节数组,如byte[] myFileByteArray = File.ReadAllBytes(filename)
标签: c# ajax web-services jquery asmx