How can I add my own custom file attributes to a h

2019-06-13 22:12发布

I have successfully created a C# project using the WebSite template within Visual Studio 2015. I modified "Default.aspx" to contain:

<asp:FileUpload ID="FileUpload1" runat="server"   />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload" PostBackUrl=".upl"/>

I also modified Default.aspx.cs to look like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Data/") + FileUpload1.FileName);
        }
    }
}

I used the Class Library template to create a DLL/Handler in order to intercept upload file web requests as shown below:

using System;
using System.Web;


namespace WebRequestInterceptorModule
{
    public class Class1 : IHttpHandler
    {


    public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            System.Collections.Specialized.NameValueCollection nvc = request.Params;

            string test = "hello";

            // Example #4: Append new text to an existing file.
            // The using statement automatically flushes AND CLOSES the stream and calls 
            // IDisposable.Dispose on the stream object.
            using (System.IO.StreamWriter file =
                new System.IO.StreamWriter(@"C:\Users\Dining Room\Documents\WriteLines2.txt", true))
            {
                file.WriteLine(test);
            }

        }
    }
}

I successfully registered the above handler by modifying web.config like so:

<system.webServer>

    <modules>
      <remove name="FormsAuthentication"/>
    </modules>

    <handlers>
      <add name="mytest" verb="*" path=".upl"  type="WebRequestInterceptorModule.Class1"/>
    </handlers>

and I know the handler is indeed intercepting upload requests because I see a file "WriteLines" generated every time I perform an upload.

So to summarize, file uploads are working correctly and I am able to successfully intercept file upload requests using a handler/DLL.

My question for the community is: Suppose I want to append a custom string attribute along with file that is being uploaded (i.e. something like a commment "these are important notes from last week") how can I do that?

In order to try to answer my own question I attempted to place a breakpoint in my handler itself and debug the "request" object. Here is what I see:

enter image description here

I see something called "QueryString" which happens to be the empty string (""). Is it possible I can put my comment in there? If so, how do I need to modify Default.apsx in order to update the QueryString?

标签: c# asp.net http
0条回答
登录 后发表回答