0

I'm developing an ASP.NET web app using VS2010,C#, I want to display a file upload control when my users click on a hyperlink (or label, it doesn't differ), and then the upload operation should be performed, I have no problem working with upload control, but currently I have an invisible upload control which display it using JavaScript in my hyperlink onclick function, the upload control is displayed but I don't know how the get the uploaded file, how should I perform this operation? I want to display upload file dialog when my users click on a label or hyperlink, then they can select their file and the file should be uploaded, and finally I should be able to work with this file in server side (I'm going to get file stream and store it in SQL), what are my options? JQuery? Ajax? JavaScript? or something else?

my JavaScript function:

function OpenFile()
{
document.getElementById("<%=fu.ClientID%>").style.display="";
var result = document.getElementById("<%=fu.ClientID%>").click();
document.getElementById("<%=fu.ClientID%>").style.display="none";
return false;
}

and my markup:

<asp:FileUpload ..... style="display:none;"....>
    <ASP:hyperlink onclick="OpenFile();"...../>
Ali_dotNet
  • 3,219
  • 10
  • 64
  • 115
  • 1
    Having a hidden control and showing it on click should work. Show your code. – nunespascal Sep 13 '12 at 03:39
  • yes the upload dialog is displayed but I don't know how to get uploaded file information – Ali_dotNet Sep 13 '12 at 03:49
  • 1
    Is your question about how to use an asp.net file upload control? – nunespascal Sep 13 '12 at 03:54
  • I've inserted my code in my question, I know how to upload files using File Upload control, rather I want to know how can I get my uploaded file in this scenario, when I use file upload control normally, I use a Submit button which performs postback and I insert my uploaded file into SQL, but here I don't know how to get the posted file, should I do a postback on my JavaScript function? – Ali_dotNet Sep 13 '12 at 03:57

2 Answers2

1

After you select a file and do a PostBack, you can access the file as follows

<asp:FileUpload ID="FileUpload1" ..... style="display:none;"....>

Access the underlying posted file using the PostedFile

    var fileLen = FileUpload1.PostedFile.ContentLength;
    Byte[] Input = new Byte[fileLen];
    myStream = FileUpload1.FileContent;
    myStream.Read(Input, 0, fileLen);

Or just save it on the server:

FileUpload1.SaveAs(savePath);
nunespascal
  • 17,584
  • 2
  • 43
  • 46
  • thanks but how can I do postabk after my upload dialog is closed? – Ali_dotNet Sep 13 '12 at 04:19
  • You must be having a button on your dialog to close. Just postback on that button(Make sure it is a server control). and do the saving on click of that button – nunespascal Sep 13 '12 at 04:30
1

I would like to recommend uploadify, it is built with flash. Very simple and easy to use with jQuery and asp.net.

Demo relies php for uploading, though it can be used with any platform including asp.net. You have to write a handler file to do the uploading, streaming and sql storage part.

Check these answers

Community
  • 1
  • 1
Sandeep Kumar M
  • 3,841
  • 3
  • 38
  • 59