【问题标题】:The given path's format is not supported不支持给定路径的格式
【发布时间】:2012-04-21 15:05:30
【问题描述】:

从本地驱动器上传文件时出现以下错误。

不支持给定路径的格式。

代码已给出。 请告诉我我必须做出哪些改变。

 string file0 = MapPathReverse(FileUpload1.PostedFile.FileName);// Get virtual path
    string conversationFileSource = Server.MapPath(file0);
    StreamReader file = new StreamReader(conversationFileSource);

【问题讨论】:

  • MapPathReverse() 函数在做什么?你能发布它的代码吗?
  • MapPathReverse 是一种获取虚拟路径的方法。
  • 公共静态字符串 MapPathReverse(string fullServerPath) { return @"~\" + fullServerPath.Replace(HttpContext.Current.Request.PhysicalApplicationPath, String.Empty); }
  • 我想上传一个文件并使用其完整路径来访问该文件。我需要它的完整路径,以便我可以读取该文件并继续。

标签: asp.net file-upload


【解决方案1】:

如果要访问上传文件的输入流:

using (StreamReader reader = new StreamReader(FileUpload1.PostedFile.InputStream))
{
    ...
}

如果您想将上传的文件保存在服务器上的某个文件夹中:

var uploadsFolder = Server.MapPath("~/uploads");
var file = Path.Combine(uploadsFolder, Path.GetFileName(FileUpload1.PostedFile.FileName));
FileUpload1.PostedFile.SaveAs(file);

【讨论】:

    猜你喜欢
    • 2011-11-13
    • 1970-01-01
    • 2017-09-14
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2017-06-26
    相关资源
    最近更新 更多