简体   繁体   中英

Unable to get a control value in ashx

I am using uploadify to upload a file. By following this tutorial:

TUTORIAL

Problem is that although I am able to get file on ASHX using

HttpPostedFile postedFile = context.Request.Files["Filedata"];

I am unable to get value of a input box on the same form:

string str = HttpContext.Current.Request.Form["Name"];

ASPX form

  <body>
        <form id="form1" runat="server"  >
            <a href="javascript:$('#<%=FileUpload1.ClientID%>').fileUploadStart()">Start Upload</a>&nbsp; 
           |&nbsp;<a href="javascript:$('#<%=FileUpload1.ClientID%>').fileUploadClearQueue()">Clear</a> 
            <div style = "padding:40px">
                <asp:FileUpload ID="FileUpload1" runat="server" />

            </div>
            <asp:TextBox MaxLength="100" CssClass="txtbox" Width="300px" ID="txtImgRemark" runat="server"> </asp:TextBox>
           <input type="Text" name="password" id="password" value="0" />
        </form>
</body>
</html>
<script type = "text/javascript">
$(window).load(
    function() {
        $("#<%=FileUpload1.ClientID%>").fileUpload({
        'uploader': 'scripts/uploader.swf',
        'cancelImg': 'images/cancel.png',
        'buttonText': 'Browse Files',
        'script': 'Upload.ashx',
        'folder': 'uploads',
        'fileDesc': 'Image Files',
        'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
        'formData': { 'Name': 'arbaaz', 'Num': 1 },
        'multi': true,
        'auto': false
    });
   }
);
</script> 

ASHX

  public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Expires = -1;
        try
        {
            HttpPostedFile postedFile = context.Request.Files["Filedata"];
            string str = HttpContext.Current.Request.Form["password"];
            string savepath = "";
             //blah blah blah

You need to add another parameter formData in the fileUpoad() function during the call like below.

$(window).load(
    function() {
        $("#<%=FileUpload1.ClientID%>").fileUpload({
        'uploader': 'scripts/uploader.swf',
        'formData': {'someKey' : 'someValue', 'someOtherKey' : 1},
        'cancelImg': 'images/cancel.png',
        'buttonText': 'Browse Files',
        'script': 'Upload.ashx',
        'folder': 'uploads',
        'fileDesc': 'Image Files',
        'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
        'multi': true,
        'auto': false
    });
   }
);

On server side

string value= context.Request["someKey"];

For more Details you can check this link: formData

I had the same problem where context.Request["Name"] gave null. This was because the HTTP post request contained a querystring AND form-data. It can't have both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM