How can I get uploaded file full path in C# 3.0?

允我心安 提交于 2019-11-29 17:40:22

You cannot get the full path of an uploaded file. This would be a privacy violation for the user that uploaded the file.

Instead, you'll need to read the Request.Files that have been uploaded. For example:

HttpPostedFile file = Request.Files[0];
using (StreamReader reader = new StreamReader(file.InputStream))
{
    while ((string line = reader.ReadLine()) != null) 
    {
        string[] addresses = line.Split(';');
        // Do stuff with the addresses
    }
}

If you are on a asp.net web page model then Server.MapPath("~/") works to get the root of the site so pass the path you need. You might need to call

HttpContext.Current.Server.MapPath("~/");

For instance a folder where the text files are saved:

string directoryOfTexts = HttpContext.Current.Server.MapPath("~/txtdata/");

To just read from it once you have it you can StreamReader it:

string directoryOfTexts = HttpContext.Current.Server.MapPath("~/txtdata/");
string path = directoryOfTexts + "myfile.txt";
string alltextinfile = "";
if (File.Exists(path)) 
{
    using (StreamReader sr = new StreamReader(path)) 
    {
       //This allows you to do one Read operation.
       alltextinfile = sr.ReadToEnd());
    }
}

If this is for a desktop application then the Applcation class has all this information:

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx

Application.StartupPath

All properties list other appdata folders and stuff but once you have the application path to the executable this gives you context such as Application.LocalUserAppDataPath.

http://msdn.microsoft.com/en-us/library/system.windows.forms.application_properties.aspx

If the content is small enough you could also just save them in a HashTable or a Generic List<String> before saving to the database as well.

var hpf = Request.Files[file] as HttpPostedFile; 

the form in the HTML should have enctype="mulitipart/form-data"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!