C# 使用fileupload控件获取文件路径
我正在使用fileupload控件在文本框中显示文本文件的内容..如果我使用C# 使用fileupload控件获取文件路径,c#,asp.net,file-upload,C#,Asp.net,File Upload,我正在使用fileupload控件在文本框中显示文本文件的内容..如果我使用 <asp:FileUpload ID="txtBoxInput" runat="server" Text="Browse" /> string FilePath = txtBoxInput.PostedFile.FileName; 但是我需要浏览按钮而不是文本框来获取路径…有什么建议吗 编辑:我的按钮单击事件 protected void buttonDisplay_Click(object sende
<asp:FileUpload ID="txtBoxInput" runat="server" Text="Browse" />
string FilePath = txtBoxInput.PostedFile.FileName;
但是我需要浏览按钮而不是文本框来获取路径…有什么建议吗
编辑:我的按钮单击事件
protected void buttonDisplay_Click(object sender, EventArgs e)
{
string FilePath = txtBoxInput.PostedFile.FileName;
if (File.Exists(FilePath))
{
StreamReader testTxt = new StreamReader(FilePath);
string allRead = testTxt.ReadToEnd();
testTxt.Close();
}
}
只有在调试模式下,但在部署应用程序时,才能从FileUpload控件获取文件名和路径。在服务器中,则无法访问,因为这是您试图通过服务器端代码访问的客户端地址
如果你真的想更改属性或重命名客户端文件,那么你可以将文件保存在服务器上的临时文件夹中,然后你可以做任何你想做的事情。@jeevan我已经尝试了你的代码,但我遇到了这个错误,在文件路径中找不到文件“C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\bala.txt”@jeevan我还得到了文件名only@Aamir在文件路径中,我也只得到文件名#Bala-您可以在试图访问FileUpload文件的地方发布您的按钮单击事件吗。@Bala:我100%确定您没有在IE中运行代码。如果是,请在IE上检查。如果您想在其他浏览器上运行,则必须为其他浏览器配置应用程序。
protected void buttonDisplay_Click(object sender, EventArgs e)
{
string FilePath = txtBoxInput.PostedFile.FileName;
if (File.Exists(FilePath))
{
StreamReader testTxt = new StreamReader(FilePath);
string allRead = testTxt.ReadToEnd();
testTxt.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string filePath,fileName;
if (FileUpload1.PostedFile != null)
{
filePath = FileUpload1.PostedFile.FileName; // file name with path.
fileName = FileUpload1.FileName;// Only file name.
}
}