获取待上传文件的绝对路径
我正在使用名为 theFile
(ASP.NET) 的简单 FileUpload 控件上传文件。我试图获取文件的绝对路径,但是 thefile.PostedFile.FileName
和 thefile.FileName
完全相同,只有文件名,没有路径!我无法使用 Server.MapPath,因为我将将此文件保存在不同的服务器上(通过 Web 服务通过字节数组传输)。
它在这一行中断:
Dim fStream As New FileStream(thefile.FileName, FileMode.Open, FileAccess.Read)
因为它正在获取文件名并将其映射到我的 VS 的相对路径!我需要绝对路径...
I am uploading a file using a simple FileUpload control named theFile
(ASP.NET). I'm trying to get the absolute path of the file, but thefile.PostedFile.FileName
and thefile.FileName
are the exact same, just the file name, no path! I can't use Server.MapPath because I will be saving this file on a different server (transferring via byte array through a webservice).
It breaks at this line:
Dim fStream As New FileStream(thefile.FileName, FileMode.Open, FileAccess.Read)
because it is taking the filename and mapping it to the relative path of my VS! I need the absolute path...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过 HTTP 上传的文件永远不会包含远程(客户端)计算机上的完整路径 - 它可能会泄露有关其目录结构的信息,因此被认为存在安全风险。另外,它有什么用呢?如果有人通过 Internet 向您上传文件,您为什么要尝试在本地(asp.net 服务器)计算机上打开文件流到其计算机上存在的路径?
上传的文件实际上作为请求的一部分以字节流的形式传输。您需要访问 FileBytes 控件的属性来获取文件,或者调用SaveAs()方法将其保存到服务器。就您而言,您可能只需获取字节并将它们发送到您需要进行的 Web 服务调用。
A file uploaded through HTTP will never contain the full path on the remote (client) machine - it could give away information about their directory structure, and so is considered a security risk. Plus, what use would it be? If someone is uploading you a file from over the internet, why would you be trying to open a filestream on your local (asp.net server) machine to the path that existed on their machine?
Uploaded files actually come through as a stream of bytes as part of the request. You need to access the FileBytes property of the control to get the file, or call the SaveAs() method to save it to the server. In your case, you could probably just get the bytes and send them off to the webservice call you need to make.
既然您当前需要文件字节数组,为什么不通过File.FileBytes 属性访问文件的字节数组呢?
以下是对 FileUpload Web 控件的 FileBytes 属性的引用: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.filebytes.aspx
如果您想直接访问 Stream 对象,您可以利用 FileContent 属性。以下是对 FileUpload Web 控件的 FileContent 属性的引用: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.filecontent.aspx
Since you're currently requiring the File Byte Array, why not access the file's Byte Array through theFile.FileBytes property ?
Here is the reference to the FileBytes property of the FileUpload web control : http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.filebytes.aspx
If you would like to access the Stream object directly, you can utilize the FileContent property. Here is the reference to the FileContent property of the FileUpload web control : http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.filecontent.aspx