C# 将文件发送到Web服务(asmx),以便将其保存在服务器上
更新问题: 我试图创建一个表单,将主题、内容和文件传递给webservices。这就是我到目前为止所做的,我想知道是否有人能告诉我我是否走在正确的方向上,以及如何做我在asmx文件中的注释中强调的部分 HTML: 如何发送文件?该文件将是一个.pdf或.doc文件,因此我可以将其与一些文本一起保存到服务器。所以,我想要一个文本框作为主题,选择file button/textbox来选择文件,然后选择submit按钮。当填写两个文本框并单击提交按钮时,它应将主题和文件发送到Web服务,Web服务将主题和文件位置保存到数据库,并将实际文件保存到服务器 此外,我正在内联网环境中开发,IE完全信任本地内联网 如何发送文件C# 将文件发送到Web服务(asmx),以便将其保存在服务器上,c#,ajax,web-services,jquery,asmx,C#,Ajax,Web Services,Jquery,Asmx,更新问题: 我试图创建一个表单,将主题、内容和文件传递给webservices。这就是我到目前为止所做的,我想知道是否有人能告诉我我是否走在正确的方向上,以及如何做我在asmx文件中的注释中强调的部分 HTML: 如何发送文件?该文件将是一个.pdf或.doc文件,因此我可以将其与一些文本一起保存到服务器。所以,我想要一个文本框作为主题,选择file button/textbox来选择文件,然后选择submit按钮。当填写两个文本框并单击提交按钮时,它应将主题和文件发送到Web服务,Web服务将
不能使用AJAX发送文件,因为使用javascript无法访问客户端计算机上的文件系统,因此无法获取文件内容。如果要将文件发送到web服务器,可以使用带有文件输入的
和enctype=“multipart/form data”
,将其发送到服务器端脚本。该脚本随后可以调用web服务,并以字节数组的形式传输文件内容。pass to method file byte arraySo类似的内容<代码>数据:{id:1,文件:'+filepath+'},然后在服务器上GetFile(int-id,byte-file)
?我不知道在ajax中该怎么做-我不是web程序员。但是您需要使用webservice,即saveFile(byte[]fileData)
并在ajax中读取文件字节数组,比如byte[]myFileByteArray=file.ReadAllBytes(filename)
阅读这些文章以获取选项:您知道如何实现这一点的教程/指南吗?请注意,我正在开发一个内联网,我已经完全信任本地内联网。@oshirowanen,你到底对哪一部分有问题?写一个
?或者调用web服务?还是别的什么?我会试试看,然后告诉你我的进展。请看我上面的尝试,告诉我我的方向是否正确。@oshirowanen,HTML部分是正确的。表单的action
属性需要修复。您不能直接发布到web服务,因为web服务不理解提交表单时此表单将发送的多部分/表单数据。您可以创建一个.ASPX页面或.ASHX处理程序,将表单的操作指向该处理程序,然后在此页面内调用web服务。
<%@ 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({
type: 'POST',
url: '/webservices/notifications.asmx/GetNotification',
data: '{id: ' + number + '}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',