Asp.net page Upload File control background Code Request. Files cannot be obtained,
During development today, the page file upload control selects a file, while the value of "Request. Files. Count" in the background is 0. I 've encountered several times in development before. I always forgot about it. I wrote it down today.
Html:
<input type="file" name="file1" >
1. Check whether the label name attribute needs to be added.
2. Add the enctype = "multipart/form-data" attribute to form.
<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
3. Add the MS_POSITIONING = "GridLayout" attribute to the body.
<body MS_POSITIONING="GridLayout">
Aspnet uploads a file to a general handler and cannot obtain the file.
This is the case if you have a problem.
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "www.w3.org/..al.dtd">
<Html xmlns = "www.w3.org/5o/xhtml">
<Head>
<Title> the html control and ashx are used to save and upload files. </title>
</Head>
<Body>
<! -- Enctype = "multipart/form-data" this type specifies the special data transmission types, such as disks and mp3 files. -->
<Form action = "Handler. ashx" method = "post" enctype = "multipart/form-data" id = "form1">
<P>
<Input id = "imgfile" name = "imgfile" type = "file"/> </p>
<P>
<Input id = "Button1" type = "submit" value = "button"/> </p>
</Form>
</Body>
</Html>
>>>>>>>>>>>>
// The following is the ashx File
<% @ WebHandler Language = "C #" Class = "Handler" %>
Using System;
Using System. Web;
Public class Handler: IHttpHandler {
Public void ProcessRequest (HttpContext context ){
Context. Response. ContentType = "text/plain ";
HttpPostedFile hpf = HttpContext. Current. Request. Files ["imgfile"]; // HttpPostedFile ...... remaining full text>
Why does the aspnet background obtain the value of the foreground html file control ??
You are mistaken. You should use JS to handle the DropDownList change event. When a single change event occurs, the number of <input type = file/> controls is dynamically changed, for example:
$ (Div). remove ($ ('# inputfile _' + $ (# DropDownList). val ()));
Changes to the file upload control do not affect page submission.
At the backend, you only need to process HttpRequest. Files.