开发者

FileUpload Returning Null

I have a FileUpload in my web application, but when "Submit" is clicked it seems to lose the file or something but not the name. So if for instance I have C:\me\Documents\myfile.jpg, I am able to capture the name myfile.jpg but when evaluating this line says "Could not find file" (in debug mode when I hover over this line)...

This is in my submit button

 if ((myID == 0 || rbUploadImg.Checked == true) && fuImage.PostedFile.FileName.Length > 0)

Here is the开发者_运维百科 code for the aspx and also checkboxes in .cs that determine weather to show the uploader or not

     <asp:Panel ID="pnlImageEdit" runat="server" Visible="false">
         <table id="tblShowImage" border="0" cellpadding="0" cellspacing="20" runat="server">
         <tr>
            <td>
             <asp:Image ID="Image1" runat="server" />
             </td>
             <td>
                 <div class="mytyle">
                    <asp:RadioButton ID="rbCurrentImage" Text="Keep Current Image" runat="server" GroupName="Product Image" AutoPostBack="True" OnCheckedChanged="rbCurrentImage_CheckedChanged" Checked="True" />

<asp:RadioButton ID="rbUploadImg" Text="Upload New Image" runat="server" GroupName="Image" AutoPostBack="True" OnCheckedChanged="rbImage_CheckedChanged" />
                                </div>
                      </table>
                </asp:Panel>
  <asp:Panel ID="pnlImageAdd" runat="server" Visible="false">
                    <div>                        
  Select an Image :<asp:FileUpload ID="fuImage" runat="server" EnableViewState="true" />

<asp:RegularExpressionValidator 
ID="RegularExpressionValidator1" 
runat="server" 
ControlToValidate="fuCollectionImage"
CssClass="Error" Display="dynamic" ValidationExpression=".*(\.[Jj][Pp][Gg]|\.[Gg][Ii][Ff]|\.[Jj][Pp][Ee][Gg]|\.[Pp][Nn][Gg])"
 ErrorMessage="Please select a valid JPEG, JPG, PNG or GIF image">
</asp:RegularExpressionValidator>
                    </div>
                </asp:Panel>

In my cs file for the checkboxes

protected void rbNewImage_CheckedChanged(object sender, EventArgs e)
{
    pnlImageAdd.Visible = true;
}

protected void rbImage_CheckedChanged(object sender, EventArgs e)
{

}

Thank you for your help


This will not work as long as you have other controls that do a post back for the page. The FileUpload control is originally use the input HTML tag with type file. When the page post back, there is no way by the server control to set the file path value in the input control. This can't be done due to security restrictions.

I suggest you do the show/hide of the panels using Javascript (client side).


I could be wrong, but I think you need to use the FileUpload.SaveAs method to be able to access it. When you press the submit button, the FileUpload is cleared of a file. You should put the file in a temporary directory to access it after the submit button is clicked.

This happens when you use an UpdatePanel as well. The UpdatePanel refreshes and the FileUpload loses the posted file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜