获取上传文件的本地路径

时间:2014-01-02 12:07:04

标签: c# asp.net .net

System.IO.Path.GetFullPath (FileUpload1.PostedFile.FileName);
Path.GetFileName (FileUpload1.PostedFile.FileName);
System.IO.Path.GetDirectoryName (FileUpload1.PostedFile.FileName).ToString ();
Convert.ToString (System. IO. Directory. GetParent (FileUpload1.PostedFile.FileName));

此代码未采用文件位置路径。它采用此路径C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\DevServer\\10.0\\kathir.doc,但谨慎的文档文件存储在D:驱动器中...请帮助如何获取正确的路径。

我的代码

public string GetDataFromFile1()
    {
        string str = string.Empty;
        if (FileUpload.PostedFile.ContentLength < 512000)
        {
            string filename = Path.GetFileName(FileUpload.FileName);
            FileUpload.SaveAs(Server.MapPath("~/") + filename);
            string filepath = Path.GetFullPath(FileUpload.PostedFile.FileName);

            DocToText docToText = new DocToText(filepath);
            string Extension = Path.GetExtension(this.FileUpload.PostedFile.FileName);
            switch (Extension.ToUpper())
            {
                case ".DOC":
                    ResumeContent = docToText.Extract();
                    break;
                case ".DOCX":
                    ResumeContent = docToText.Extract();
                    break;
                case ".PDF":
                    ResumeContent = docToText.ExtractPDFtoWord();
                    break;
                default:
                    break;
            }
            str = "File uploaded successfully!";
        }
        return str;
    }

1 个答案:

答案 0 :(得分:1)

我认为问题出在这一行:

string filepath = Path.GetFullPath(FileUpload.PostedFile.FileName);

您需要使用上一行中的值,如下所示:

string path = Server.MapPath("~/") + filename;
FileUpload.SaveAs(path);
string filepath = Path.GetFullPath(path);

现在可能不需要最后一行了。