Showing posts with label fileupload. Show all posts
Showing posts with label fileupload. Show all posts

Monday, March 26, 2012

UpdatePanel & Wizard together ERROR

I have a Wizard control inside of the UpdatePanel control. In the Wizard control, I have a FileUpload control. WHen I set the ScriptManager to enable Partial Rending, I get this error message when I tried to upload the file:

"Object reference not set to an instance of an object"

This is the culprit:

int imageSize = fileuploader.PostedFile.ContentLength;

Because fileuploader is NULL.

Why does this happen? Does the page not instantiate a fileupload object when the whole page is not rendered?

thanks!

Lots of threads on this issue ... Unfortunately you can't use a FileUpload control inside of an update panel. FileUploading, in general, has always been a thorny issue. The form has has to do an HTTP post to include the bytes of the file selected by the user. No indication that this can (or should) be fixed.

With a normal "full" form postback, the browser (running natively on the operating system of the host system) is able to obtain the binary of the file selected by the user and push those bytes to the server in an HTTP POST.

Allowing arbitrary JavaScript to perform the same operation -- whether it's Atlas or any other Ajax framework -- would violoate the security lockdowns that most modern browsers *must* enforce. If JavaScript was able to programmatically probe the local file system of the user and upload binary data to a server, an entirely new/old form of exploits would be enabled.

It's for the similar reasons that you can't programmatically set the default file that you'd like to upload in the web page. Use of FileUpload and the underlying HTML tags requires an "act of comission" on the part of the user. If this was not enforced by the browser, a malicious web page could data-mine arbitrary local storage w/out any knowledge/awareness of the user.

What should work would be to render in a asp:Literal control the raw HTML <input type="file" .../> tag. Going down this path would require that you manually implement an ASPX file that can accept the resulting HTTP post and process the response -- exactly what the FileUpload control is actually doing under the covers.
After many hours of trying to get a fileUpload control to work from inside an update panel i discovered that it isn't possible.

However, from all that I can gather, it should work from outside anupdate panel, even if there is a different update panel on the page?

Can someone confirm for me that this is the case?

Many Thanks,
Dave

hello.

yes, this is the case. the upload control should work when pleced outside the updatepanel.


what if the FileUpload component is inside an iframe inside an update panel?would it help to trigger an full page postback?

As I know that the popup control is using an iframe, please try if u guys still want to...


I think you should take a look by overwritting the methods loadControlState and savecontrolstate, to maintain the fileupload's state. I am a newbie, but maybe it could work.

UpdatePanel + FileUpload + PostBackTrigger doesnt seem to work

As the title of the thread states, I have a file upload control inside an update panel and added a postbacktrigger to activate a full post back when the submit button is pressed but everytime I try and upload a file the HasFile property never equals true even though I know I chose a file with the file upload control. Am I missing something essential? Here is a snippet of the code..

1<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true">2 <ContentTemplate>34 ...5 ...6 ...78 </ContentTemplate>910 <Triggers>11 <asp:PostBackTrigger ControlID="PropertyValueSubmitButton" />12 </Triggers>1314</asp:UpdatePanel>

Hi There,

As what i can see, your code seem to be alright.

I even run a test base on your scenaria.

<asp:UpdatePanelID="UpdatePanel1"runat="server"ChildrenAsTriggers="true">

<ContentTemplate>

<asp:FileUploadID="FileUpload1"runat="server"/>

<asp:ButtonID="btnPostBack"runat="server"Text="My PostBack Button"OnClick="btnPostBack_Click"/>

</ContentTemplate>

<Triggers>

<asp:PostBackTriggerControlID="btnPostBack"/>

</Triggers>

</asp:UpdatePanel>

In code behind, i set a break point and test the value is true when i select a file and post it by click on the button

protectedvoid btnPostBack_Click(object sender,EventArgs e)

{

Boolean b = FileUpload1.HasFile;

}


Hi kbeeveer46,

Has your problem been resolved yet? If yes , sharing your work will be greatly appreciated!


Hi did you find a solution to your problem. I experience the same problem.

/Frederik


Hi,

I trying the exakt same thing with an AsynPostBackTrigger and then the FileUpload1.HasFile returns false. Do you know why this is so. What am I doing wrong.


I ran into almost exactly the same problem, although mine was a little more difficult because my File Upload control was in a dynamically loaded User Control and couldn't be moved outside of the Update Panel. I set the PostBackTrigger so that everything inside the userconrol would post back. I ran into the same problem that the .HasFile value was always false/null on the first page load. If I posted the page back with any button, then the upload control would work.

I had another AJAX application that was set up almost identical and it was working fine after setting the postbacktrigger. I started looking for differences, and in my default.aspx in the working app I found the following directive on the Form statement:

enctype="multipart/form-data"

Looks like this:

<formid="Form1"method="post"enctype="multipart/form-data"runat="server">

I'm not sure why I ever put it there, and I read on another post where this shouldn't matter, but guess what... I put that in my default.aspx and tried the non-working app again, and viola. Working like a charm.


I never did find a solution but I will definitely try out what hartmacw has said.

EDIT: hartmacw's solution worked perfectly. Thanks.

Saturday, March 24, 2012

UpdatePanel and FileUpload problem.

Hi, all

I got a question.

I have a FileUpload component outside a UpdatePanel Control, and a Button named "Upload" inside a UpdatePanel.

When I clicked Upload, and get the FileName property of FileUpload1, it contains nothing. And the HasFile property is False.

What's wrong with it? And how to handle it?

thanks.

File uploads do not work inside of updatepanels. The button you are clicking is causing a postback, but it has nothing to do with the upload file control textbox and what the path is to the file.

To get ajax style behavior from the file upload consider using the iframe technique.

http://encosia.com/2007/02/23/ajax-file-downloads-and-iframes/

Good Luck,

Louis


FileUpload controls will work with UpdatePanels, but you must set the button as a PostBackTrigger instead of an AsynchPostBackTrigger.

------------------
When you ask a question, remember to click "mark as answered" when you get a reply which answers your question.


thanks


I have already tried to set a PostBackTrigger instead of AsyncPostBackTrigger, but it still not work.

<asp:UpdatePanel ID="upFile" runat="server" UpdateMode="Conditional" RenderMode="Inline">
<Triggers>
<asp:PostBackTrigger ControlID="btnUpload" />
</Triggers>
<ContentTemplate>
<asp:FileUpload ID="File1" runat="server" Style="display: none" onpropertychange="ctl00_cphMain_txtFile.value = this.value" />
<asp:Button ID="btnUpload" runat="server" Text = "upload" OnClick="btnUpload_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:TextBox ID="txtFile" runat="server" ReadOnly="true" />
<asp:Button ID="btnFile" runat="server" Text="Browse" OnClientClick="javascript:document.getElementById('ctl00_cphMain_File1').click(); return false;" />

ps: I uses a MasterPage, any questions with it?


FileUpload controls is not compatible with UpdatePanel when they are used to upload files as part of an asynchronous postback

See this

http://geekswithblogs.net/rashid/archive/2007/08/01/Create-An-Ajax-Style-File-Upload.aspx


It seems that I can't get FileUpload and UpdatePanel work together..

thanks all

UpdatePanel and FileUpload control revisited one more time

I've seen nothing but real janky solutions to the problem at hand. That problem being that fileupload doesnt appear to be working on an update panel.

I've done a signifigant ammount of research and understand WHY it occurs, but not if theres any real way to circumvent the problem. I was wondering if anyone has been able to make a signifigant difference where fileupload could occur and not initiate a full postback.

I am using july ctp.

Anything that can be done?

I ran into exact problem. Only solution, by my opinion is to use client button control, and manage fileupload with javascript. Unfortunatelly I don't know javascript nor I have time to learn it :(
What is the problem putting a (invisible) button outside the update- panel and the upload- button inside the panel. Then you add a new eventhandler to the outside- button and process the things you want to there (save the file, show the file, redirect, ...).

The last thing you have to do is to trigger the outside- button event if the inner button is clicked. you just have to add a onclick- attribute: InnerButton.Attributes.Add("onclick", Page.ClientScript.GetPostbackEventReference(OutsideButton, ""))

ollifant

Nice, but there are still few problems:

- fileUpload control has its property "hasFile" set to'true' only if second time fire button click event (first time nothing happened)

- page still refreshs whole content even if i add button to trigger collection of update panel

forgive me my poor english


FileUpload controls when they are used to upload files as part of an asynchronous postback.

See for more details..

http://ajax.asp.net/docs/overview/UpdatePanelOverview.aspx

Updatepanel and fileupload

hi !

in a webpage i use a fileupload wich is used to upload a xls file, and do some stuff on it and write some text in a label to say if it works

everything works fine

I tried to put the fileupload, button and a trace label in an updatepanel but i got a problem :

when i hit the button and debug the sub i see that the fileupload.hasfile is always False !

can you help me ??

without a full postback you won't be able to send the file to the server.


thanx for the reply, is there no way to use ajax technologie to post the file ?

do you thing it will be possible in the futur ?


Thanx


thanx for the reply, is there a way to use ajax technologie to post the file ?

do you thing it will be possible in the futur ?


Thanx

UpdatePanel and FileUpload

Hi,

i have wide customer table that i handle with a big DetailView nested inside an UpdatePanel for faster interaction. Works like a charm.

Now they ask me to allow the user to upload a file, but when i add it in the detail view and i save data i get no file at all, as if putting it into an updatepanel do not allow him to send the file any more.

Is there some setting i have to put so to make it work ?

thanks in advance,

Fabrizio

Hi There,

This is a known ** not sure should call it issue, but callback will not work for FileUpload ( e.g. have fileupload in updatepanel )

The trick is assign postbacktrigger to the button that upload the file.

Example:

<asp:ScriptManagerID="ScriptManager1"runat="server">

</asp:ScriptManager>

<asp:UpdatePanelID="UpdatePanel1"runat="server">

<Triggers>

<asp:PostBackTriggerControlID="Button1"/>

</Triggers>

<ContentTemplate>

<asp:FileUploadID="FileUpload1"runat="server"/>

<asp:ButtonID="Button1"runat="server"Text="Button"OnClick="Button1_Click"/>

</ContentTemplate>

</asp:UpdatePanel>


Hi,

Thanks for your help, after posting i found other posts on the problem, for future references i add here another help to the problem of the button not sending the file on the first click.

try to use this

in Form tag add this

enctype="multipart/form-data"

example:

<form id="form1" enctype="multipart/form-data" runat="server">

i use that and it works

As inhttp://forums.asp.net/t/1060363.aspx?PageIndex=2


Thank you! I've been fighting with this for a week!
Thanks for the tip!!!!