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();"...../>
4
  • 1
    Having a hidden control and showing it on click should work. Show your code. Commented Sep 13, 2012 at 3:39
  • yes the upload dialog is displayed but I don't know how to get uploaded file information Commented Sep 13, 2012 at 3:49
  • 1
    Is your question about how to use an asp.net file upload control? Commented Sep 13, 2012 at 3: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? Commented Sep 13, 2012 at 3:57

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks but how can I do postabk after my upload dialog is closed?
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
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.