Favorites: small experience in ASP. NET 2.0 multi-File Upload

Source: Internet
Author: User
ASP. NET 2.0 small experience in uploading multiple files

To upload any number of files, click the button to add a file upload box, which was previously viewed on the network hard disk. Javascript: I know how to add any file upload control. The problem is that the HTML control is added. I don't know how to get files from the server.

So Google searched for "ASP. net multi-File Upload ", I really found a file named" in ASP. net to achieve multi-File Upload, the article is VB. net implementation, the function is exactly the same as what I want, I mainly want to see how the server side obtains the files uploaded by the client, read the code in the text, the original is so simple, system. web. httpcontext. current. request. files contains the files uploaded by the client browser. I used C # To write a simple piece of code. I thought it was okay. unexpectedly, I uploaded three images, system. web. httpcontext. current. request. files returns 0 files.

I don't know why. Let's look at the code. It's impossible to make some mistakes. Let's look at several other articles in the Google search results and find that the first one I read is not the original one, the original Instance on the author's website has two versions, one of which is VB. net is a C # program. Now I don't have to write it myself. Copy the original code to the local machine and run it. That's OK. Why can't I write the code? I repeatedly compared the differences between my code and the code in the article, tried a few places, and finally found that it had nothing to do with other places, the reason is that the enctype attribute of <Form ID = "form1" runat = "server" enctype = "multipart/form-Data"> does not exist on the page created by VS 2005, in the example of this article, I added enctype = "multipart/form-Data" to the system. web. httpcontext. current. request. files.

It is probably because the page created in vs 2003 has this attribute by default. Otherwise, the author of such an important attribute should mention it in the article.

Refer:
Multifile upload in ASP. NET

The test code I have done:

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "default. aspx. cs" inherits = "demo. _ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Script language = "JavaScript">
Function AddFile ()
{
VaR STR = '<input type = "file" size = "50" name = "file">'
Document. getelementbyid ('myfile'). insertadjacenthtml ("beforeend", STR)
}
</SCRIPT>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server" enctype = "multipart/form-Data">
<Input type = "button" value = "add" onclick = "AddFile ()">
<Input onclick = "This. Form. Reset ()" type = "button" value = "RESET (reset)">
<Asp: button runat = "server" text = "Upload" id = "Upload" onclick = "upload_click1"> </ASP: button>
<Div id = "myfile">
<Input type = "file" name = "file"/>
</Div>
</Form>
</Body>
</Html>

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Namespace demo
{
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void upload_click1 (Object sender, eventargs E)
{

Httpfilecollection _ FILES = system. Web. httpcontext. Current. Request. files;

For (INT I = 0; I <_ FILES. Count; I ++)
{
_ FILES [I]. saveas (server. mappath ("~ /Files/"+ _ FILES [I]. filename ));
}
}
}
}

Reference from http://bg5sbk.cnblogs.com/archive/2006/06/11/mulitfileuploadtest.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.