获取使用FileUpload控件的一个文件的路径 [英] Getting the path of a file using fileupload control

查看:681
本文介绍了获取使用FileUpload控件的一个文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是FileUpload控件在textbox..if我用它来显示文本文件的内容,这个

  < ASP:文件上传ID =txtBoxInput=服务器文本=浏览/> 

字符串的文件路径= txtBoxInput.PostedFile.FileName;

这将只得到文件名如 bala.txt 。我需要这样的 D:\New Folder\bala.txt



而不是文件上传的控制我已经使用文本框来得到这样 D的路径:\New Folder\bala.txt

 < ASP:文本框ID =txtBoxInput=服务器WIDTH =451px>< / ASP:文本框> 

字符串的文件路径= txtBoxInput.Text;



但我需要浏览按钮,而不是文本框来获取路径...任何建议??



编辑:我的按钮点击事件


 >保护无效buttonDisplay_Click(对象发件人, EventArgs五)
{
字符串的文件路径= txtBoxInput.PostedFile.FileName;
如果(File.Exists(文件路径))
{
StreamReader的testTxt =新的StreamReader(文件路径);
串ALLREAD = testTxt.ReadToEnd();
testTxt.Close();
}
}


解决方案

您可以从FileUpload控件文件名和路径只有当你在调试模式,但是当你部署你的应用程序。在服务器上,那么你不能因为这是你试图通过服务器端代码访问您的客户端地址。

 保护无效的button1_Click(对象发件人,EventArgs五)
{
字符串文件路径,文件名;
如果(FileUpload1.PostedFile!= NULL)
{
=文件路径FileUpload1.PostedFile.FileName; //与路径文件名。
文件名= FileUpload1.FileName; //只有文件名。
}
}

如果你真的想更改属性或重命名客户端文件然后就可以保存上的临时文件夹服务器上的文件,那么你可以做你想要的所有东西。


I am using a fileupload control to display the contents of a text file in a textbox..if i use this

<asp:FileUpload ID="txtBoxInput" runat="server" Text="Browse" />

string FilePath = txtBoxInput.PostedFile.FileName;

it will get only the file name like bala.txt.i need like this D:\New Folder\bala.txt

Instead of fileupload control i have used textbox to get the path like this D:\New Folder\bala.txt

<asp:TextBox ID="txtBoxInput" runat="server" Width="451px"></asp:TextBox>

string FilePath = txtBoxInput.Text;

But i need browse button instead of textbox to get the path...Any Suggestion??

EDIT:My button click event

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();
    }
}

解决方案

You can get the file name and path from FileUpload control only when you are in debug mode but when you deployed your app. in server then you cant because that is your client address which you are trying to access by server side code.

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.
    }
}

If you really want to change properties or rename client file then you can save file on server on temp folder then you can do all thing which you want.

这篇关于获取使用FileUpload控件的一个文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆