Get the absolute path of a file to be uploaded

旧街凉风 提交于 2019-12-24 06:31:40

问题


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


回答1:


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.




回答2:


(transferring via byte array through a webservice).

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



来源:https://stackoverflow.com/questions/1297939/get-the-absolute-path-of-a-file-to-be-uploaded

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