0

I have file upload control inside update panel and repeater control. Repeater control has an image button. When this button is pressed the postback occurs and File upload control has no value. I used postback trigger to store fileupload control in session when postback occurs. At the time of page load we are assigning fileupload control from session but Fileupload control shows No file chosen instead of selected file name.

              <tr>
                <td>
                 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                    <ContentTemplate>
                                        <asp:FileUpload ID="FileUpload1" runat="server" CssClass="text"/>

                                    </ContentTemplate>
                                    <Triggers>
                                       <asp:PostBackTrigger ControlID="ApplicationUrl_Repeater" />
                                    </Triggers>
                                </asp:UpdatePanel>
                            </td>
                        </tr>

                        <tr runat="server" visible="false" id="ApplicationUrl">
                                                          <td>
                                <asp:Repeater ID="ApplicationUrl_Repeater" runat="server" OnItemCommand="RepeaterItemCommmand11" OnItemDataBound="RepeaterItemDataBound11" Visible="True">
                                    <ItemTemplate>
                                        <table>
                                            <tr>
                                                <td>
                                                    <asp:TextBox ID="Repeater_ApplicationUrl_TxtBox" Text='<%# Eval("Url") %>' runat="server" CssClass="text" Width="200" MaxLength="1024"></asp:TextBox>
                                                    <asp:DropDownList ID="Repeater_ApplicationType_DropdownList" runat="server"></asp:DropDownList>
                                                    <asp:ImageButton ID="Repeater_ImgButton" runat="server" CssClass="AddIcon" Height="15px" ImageAlign="Bottom" />
                                                </td>
                                            </tr>
                                        </table>
                                    </ItemTemplate>
                                </asp:Repeater>
                            </td>
                      </tr>


 protected void RepeaterItemCommmand11(object source, RepeaterCommandEventArgs e)
    {
       if (Session["FileUpload1"] == null && FileUpload1.HasFile)
        {
            Session["FileUpload1"] = FileUpload1;
        }
    }

 protected void Page_Load(object sender, EventArgs e)
 {
     if(Page.IsPostBack)
     {
         if (Session["FileUpload1"] != null && !FileUpload1.HasFile)
        {
            FileUpload1 = (FileUpload)Session["FileUpload1"];
            Session.Remove("FileUpload1");
        }
     }
 }

I want fileupload control should display file name after postback. Please suggest.

Thanks in advance.

Manish Jain
  • 865
  • 3
  • 13
  • 28
  • https://stackoverflow.com/questions/15829337/how-to-get-or-retain-file-upload-control-value-after-postback – CodeCaster Jan 15 '18 at 10:43
  • @CodeCaster - Is there any other way to store file on server? – Manish Jain Jan 15 '18 at 10:54
  • Possible duplicate of [how to get or retain file upload control value after postback](https://stackoverflow.com/questions/15829337/how-to-get-or-retain-file-upload-control-value-after-postback) – VDWWD Jan 15 '18 at 10:55
  • @VDWWD - No it is not duplicate. problem is different here. – Manish Jain Jan 15 '18 at 10:58
  • @VDWWD - To maintain fileupload control i used session but still on UI it shows No File chosen. That's the different one. – Manish Jain Jan 15 '18 at 11:04
  • You are somewhat right. But you cannot set a value from code behind (or PostBack) – VDWWD Jan 15 '18 at 11:04
  • @VDWWD - Not possible to store files at server side client will not allow it. Please advise another way to solve this. – Manish Jain Jan 15 '18 at 11:06
  • 1
    Why are you making a form that allows for file upload if you can't store them on the server? Read [ask] and properly explain your problem. You can't assign a file upload control from the server. – CodeCaster Jan 15 '18 at 11:13
  • 2
    @ManishJain. Then there is no solution other that asking the client to select the file again (or only show the FileUpload Control on a PostBack before final form submition) – VDWWD Jan 15 '18 at 11:14
  • "Not possible to store files at server side client will not allow it." Then why have you got a file upload control? What is it for? It makes no sense, if you aren't allowed to have files uploaded. Perhaps I have misunderstood something. – ADyson Jan 15 '18 at 11:24
  • Can we set fileuploadcontrol filename using javascript if yes please suggest how? – Manish Jain Jan 15 '18 at 11:49
  • You didn't used to be able to. See here for why: https://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html . However, as of 2017, you can do so in certain circumstances (although possibly not your specific use case): https://stackoverflow.com/questions/47515232/how-to-set-file-input-value-when-dropping-file-on-page Pretty sure you could have googled this yourself :-(. – ADyson Jan 15 '18 at 11:55
  • But again, why have you even got a file upload control, if you're not allowed to upload files to the server?? – ADyson Jan 15 '18 at 11:55
  • @ADyson - We upload file using Fileuploadcontrol and pass file stream to web API. Web API parse it to c# object and store in database. – Manish Jain Jan 15 '18 at 12:32
  • @ADyson - I tried document.getElementById("Fileupload").files[0] = "Amit.json"; It is not working Please suggest. – Manish Jain Jan 15 '18 at 15:55
  • 1
    Did you actually read those links I sent you? Big yellow box in the answer says "You can only do this if you have an existing FileList OR dataTransfer object that you are able to set to the input file element (as the setter method **DOES NOT accept plain text strings**)." (my bold) – ADyson Jan 15 '18 at 15:57

0 Answers0