Click here to Skip to main content
11,721,385 members (83,166 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET
Using this code I'm uploading the file and showing on gridview. But when I click the upload button nothing happen and no error come....

I have checked by debugging when I select the file nothing comes to the object of HttpFileCollection means hfc gives null.

Please help me

protected void btnUpload_Click(object sender, EventArgs e)
    HttpFileCollection hfc = Request.Files; // htc gives null
    foreach (string filetagname in hfc)
        HttpPostedFile hpf = Request.Files[filetagname];
        if (hpf.ContentLength > 0)
            int size = hpf.ContentLength;
            string name = hpf.FileName;
            int position = name.LastIndexOf(@"\\");
            name = name.Substring(position + 1);
            string content = hpf.ContentType;
            byte[] filedata = new byte[size];
            hpf.InputStream.Read(filedata, 0, size);
            filedownload.savefile(name, content, size, filedata);
    DataTable dt = filedownload.GetFileList();
    GridView1.DataSource = dt;
Posted 8-May-13 0:16am
Edited 8-May-13 1:41am
Johnny J.131.7K
Tadit Dash at 8-May-13 6:55am
What are you using for file upload control input type="file" or asp:FileUpload ?
Member 7909184 at 8-May-13 6:58am
i'm using input type="file"
Member 7909184 at 8-May-13 7:12am
Tadit Dash at 9-May-13 1:21am

Follow my answer. Please accept and upvote.

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hey see HttpRequest.Files Property[^]
It shows a good example of using this functionality, Hope this helps.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Follow the answer Upload more than one file C#[^].
As far as I can see, you just need to add enctype="multipart/form-data" to your form:

You are not using asp:FileUpload control which adds that enctype automatically, so you should do that manually.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 510
1 OriginalGriff 249
2 Mika Wendelius 170
3 F-ES Sitecore 80
4 cyycoish 70
0 Sergey Alexandrovich Kryukov 2,209
1 OriginalGriff 969
2 F-ES Sitecore 660
3 Maciej Los 609
4 Richard MacCutchan 560

Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 8 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100