0

我有一个包含 3 个文件输入的网页。当用户上传文件时,表单上有特定的字段需要发送。我无法弄清楚如何将自定义数据添加到我的 POST 以及如何在服务器上检索它。这就是我的代码的样子:

具有 3 个文件输入和其他文本框/下拉列表的 ASPX 页面:

<form action="FilesUploader.ashx" method="post">

    <div id="dvNewAttachment1">
        <span>Attachment Type</span>
        <select id="ddlAttachmentType1">
            <option>T1</option>
            <option>T2</option>
            <option>T3</option>
        </select>
        <span>Description</span>
        <input id="txtDesc1" />
        <select id="ddlApproval1">
            <option>Yes</option>
            <option>No</option>
        </select>
        <input id="fileUploader1" type="file" runat="server" />
    </div>
    <br />
    ---------------
    <div id="dvNewAttachment2">
        <span>Attachment Type</span>
        <select id="ddlAttachmentType2">
            <option>T1</option>
            <option>T2</option>
            <option>T3</option>
        </select>
        <span>Description</span>
        <input id="txtDesc2" />
        <select id="ddlApproval2">
            <option>Yes</option>
            <option>No</option>
        </select>
        <input id="fileUploader2" type="file" runat="server" />
    </div>
    <br />
    -------------------------------
    <div id="dvNewAttachment3">
        <span>Attachment Type</span>
        <select id="ddlAttachmentType3">
            <option>T1</option>
            <option>T2</option>
            <option>T3</option>
        </select>
        <span>Description</span>
        <input id="txtDesc3" />
        <select id="ddlApproval3">
            <option>Yes</option>
            <option>No</option>
        </select>
        <input id="fileUploader3" type="file" runat="server" />
    </div>


<input type="submit" />

</form>

这就是我的处理程序的样子:

public void ProcessRequest(HttpContext context)
        {

            HttpPostedFile myFile = context.Request.Files[0];

            int nFileLen = myFile.ContentLength;

            byte[] buffer = new byte[nFileLen];

            using (BinaryReader br = new BinaryReader(myFile.InputStream))
            {
                br.Read(buffer, 0, buffer.Length);
            }

        }

如您所见,我有 3 个上传,每个都有与之关联的附件类型和描述,我需要为处理程序中的每个输入文件检索它们。

现在我只是处理来自第一个输入的文件,但稍后我将循环输入并处理它们。

4

1 回答 1

0

如果您的处理程序没有收到您放置在文件上传器中的文件,那么您的表单需要具有如下附加属性:

<form action="FilesUploader.ashx" method="post" enctype="multipart/form-data">

为了从服务器上的下拉列表中获取值,您需要为name它们提供一个属性:

<select id="ddlApproval1" name="ddlApproval1">
    <option>Yes</option>
    <option>No</option>
</select>
于 2012-04-05T14:30:01.280 回答