摘要:Fileupload路徑消失問題解決
aspx 一個TextBox 一個FileUpload 一個 Button
<dx:ASPxTextBox ID="TextBox" runat="server" Enabled="False" Visible="False" Width="300px" Height="19px">
</dx:ASPxTextBox>
<asp:FileUpload ID="FileUpload" runat="server" Width="300px" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"/>
<dx:ASPxButton ID="Button" runat="server" Text="重新選擇檔案" Visible = "false"
onclick="xbtnFilebghd_Click">
<ClientSideEvents Click="function(s, e) {
LoadingPanel.Show();
}" />
</dx:ASPxButton>
aspx.cs----------------------------------------------------------------------------------------------------------------------------------
string m_st = "";
if (ViewState["file"] != null)
{
if (ViewState["file"].ToString() != "")
m_st= ViewState["file"].ToString();
}
//上傳檔案
if (m_st == "") //m_st有值,表示此檔案不用重存
{
if (this.FileUpload.HasFile)//判斷檔案是否存在
{
if (CheckFiles("FileUpload")) //檢查檔案類型
{
m_st = FileUpload.PostedFile.FileName;
//記錄檔案名稱
TextBox.Text = m_st;
TextBox.Visible = true;
if (!CommLib.CheckFolder(strPath, true))
{
showPanelMsg("檔案上傳失敗!!");
return m_return = "";
}
FileInfo m_fileInfo = new FileInfo(m_st);
m_st = Guid.NewGuid().ToString()+ m_fileInfo.Extension;
while (true)
{
if (!File.Exists(m_inside_path + m_fileInfo.Extension + m_st)) break;
m_st = Guid.NewGuid().ToString()+ m_fileInfo.Extension;
}
strSaved_Path = Path.Combine(strPath, m_st);
FileUpload.SaveAs(strSaved_Path);
}
else
{
showPanelMsg(String.Format("{0}非gif,png,jpeg,jpg副檔名!!", FileUpload.PostedFile.FileName));
return m_return = "";
}
}
}
if (m_st != "")
{
ViewState["file"] = m_st;
FileUpload.Visible = false;
Button.Visible = true;
}
重新選擇按鈕
protected void Button_Click(object sender, EventArgs e)
{
string m_msg = "";
try
{
FileUpload.Visible = true;
Button.Visible = false;
ViewState["file"] = "";
TextBox.Text = "";
TextBox.Visible = false;
}
catch (Exception ex)
{
m_msg = ex.Message.ToString();
}
finally
{
if (m_msg == "")
ScriptManager.RegisterStartupScript(Page, typeof(Page), "Update script", "LoadingPanel.Hide();", true);
else
ScriptManager.RegisterStartupScript(Page, typeof(Page), "Update script", "alert('" + m_msg + "');LoadingPanel.Hide();", true);
}
}