如何解决回发问题文件上传控件的路径不存在

时间:2013-04-25 05:55:09

标签: c# .net

我使用asp.net fileupload控件上传文件,每当由于页面上的其他字段发生回发时,文件上传控件中的选定路径丢失。我在页面上使用多个文件上传控件来进行差异。目的如何解决这个问题?请帮忙简单&合适的例子亲爱的。

string file = Path.GetFileName(UploadSalesServiceCopy.PostedFile.FileName);
string filepath2 = ConfigurationManager.AppSettings["ServerMapImgPath"].ToString();//.......local drive path via web config
string subPath = filepath2 + file;
bool IsExists = System.IO.Directory.Exists(Server.MapPath(subPath));
if (!IsExists)
    System.IO.Directory.CreateDirectory(Server.MapPath(subPath));

if (UploadSalesServiceCopy.HasFile)
{
    //UploadSalesServiceCopy.SaveAs(subPath);//PHYSICAL path
    UploadSalesServiceCopy.SaveAs(Server.MapPath(subPath));//server path             
}
else
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "javascript", "alert('No File Selected.')", true);
}

3 个答案:

答案 0 :(得分:0)

点击上传按钮时... 您的页面首先重定向到Page_load事件,您可以在其中设置页面值。

中的那个
page_load(..)
{
   if(!IsPostBack)
   {
      ..setpagevalues();
   }
}

那主要的共鸣......你得到了无效的道路...... 试试吧..

答案 1 :(得分:0)

如果您使用带有更新面板的asp.net文件上传控件。那么问题是 文件上传控件与部分回发不兼容。

<asp:FileUpload ID="fpTest1" runat="server" />
<asp:FileUpload ID="fpTest2" runat="server" />
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnActions" runat="server" Text="Other Actions" >
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="test" runat="server" OnClick="test_Click" Text="Upload File" />

您可以看到,当您点击其他操作时,按钮文件上传将具有值。

将Post back控件放在Update Panel中,并将文件上传控件输出 更新面板。

答案 2 :(得分:0)

您只需将fileupload与 updatepanel 一起使用,并将 ViewStateMode =&#34;已启用&#34; 添加到文件上传。

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" ViewStateMode="Enabled"/>
 </ContentTemplate>
        </asp:UpdatePanel>