【问题标题】:Sending files to a webservice (asmx) so it can be saved on the server将文件发送到 web 服务 (asmx),以便可以将其保存在服务器上
【发布时间】: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


【解决方案1】:

如何发送文件?

您不能使用 AJAX 发送文件,因为使用 javascript 您无权访问客户端计算机上的文件系统,因此无法获取文件内容。如果您想将文件发送到网络服务器,您可以使用带有文件输入的&lt;form&gt;enctype="multipart/form-data",它将发布到服务器端脚本。然后,此脚本可以调用 Web 服务并将文件内容作为字节数组传输。

【讨论】:

  • 你知道如何实现这个的教程/指南吗?请注意,我正在一个 Intranet 上开发,我已将 IE 完全信任给本地 Intranet。
  • @oshirowanen,您到底对哪一部分有问题?写&lt;form action="/foo.aspx" enctype="multipart/form-data" method="post"&gt;&lt;input type="file" name="file" /&gt;&lt;input type="submit" value="Upload"/ &gt;&lt;/form&gt;?还是调用网络服务?还是别的什么?
  • 我会试一试,然后告诉你我的进展情况。
  • 请看我上面的尝试,如果我朝着正确的方向前进,请告诉我。
  • @oshirowanen,HTML 部分是正确的。需要修复的是表单的action 属性。您不能直接发布到 Web 服务,因为 Web 服务不理解 multipart/form-data,这是您提交时此表单将发送的内容。您可以创建一个 .ASPX 页面或一个 .ASHX 处理程序,您可以将表单的 action 指向它,然后从该页面中调用 Web 服务。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
  • 2018-08-30
  • 1970-01-01
  • 2014-11-17
相关资源
最近更新 更多