从文件 HTML 输入的代码隐藏中获取完整文件路径
我使用文件 HTML 输入而不是文件上传 Web 控件。别问为什么,但我必须这么做!
HTML:
<input type="file" id="image1" class="listUploadAdd" name="ImageAdd1" />
代码隐藏:
Dim ImageAdd1 As String = Request.Form("ImageAdd1").ToString()
我从“C:/Orange.jpg”浏览上传,代码隐藏中的结果只是图像名称“Orange.jpg”,而不是从本地驱动器获取要上传的文件所需的完整“C:/Orange.jpg”。
从代码后面捕获完整图像路径并将其上传到服务器的最佳方法是什么?
谢谢。
I used file HTML input instead of FileUpload Web Control. Don't ask why but I just have to!
HTML:
<input type="file" id="image1" class="listUploadAdd" name="ImageAdd1" />
Code behind:
Dim ImageAdd1 As String = Request.Form("ImageAdd1").ToString()
I browsed from "C:/Orange.jpg" to upload and the result in Code Behind is just the image name "Orange.jpg" and not the full "C:/Orange.jpg" which is needed to get the file from local drive to be uploaded.
What is the best way to capture the full image path from code behind to be uploaded onto the server?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
runat="server"
添加到输入元素吗? :) 那么将文件存储在服务器上就非常容易了。如果您想要原始文件名和路径,请尝试以下操作:
要在不使用
runat="server"
属性的情况下下载文件,您可以执行以下操作:但是你必须在html页面的form元素上设置
enctype
:Can you add a
runat="server"
to the input element? :) Then it's pretty easy to store the file on your serverIf you want the original filename and path, try this:
To download the file without using a
runat="server"
attribute, you can do this:But you have to set the
enctype
on the form element in the html page:您将无法从客户端计算机上提取文件。当您开发程序时,客户端和服务器是同一台机器,但当您部署它时,服务器和客户端将是不同的机器。当后面的代码执行时(在服务器上),如果您尝试打开 C:\Orange.jpg,您将尝试从服务器的硬盘上打开它。该文件可能不存在。
当您从网页上传文件时,该文件将作为 POST 消息的一部分发布到服务器。您可以从 Form 集合中获取该文件。您无需将上传器转换为 ASP.NET 控件,也无需添加
runat="server"
属性。只要您发布包含输入元素的表单,它将被提交到服务器。文件的内容将作为字节数组存储在表单中。您可以将此字节数组作为文件保存到服务器硬盘的某个位置。
总之:
您不需要知道客户端计算机上文件的路径,因为您无论如何都无法访问它。使用作为表单提交的一部分发布的文件数据来在服务器上保存文件的副本。
You won't be able to pull the file off the client's machine. When you're developing your program, the client and the server are the same machine but when you deploy it, the server and client will be different machines. When the code behind executes (on the server), if you try to open C:\Orange.jpg you will be trying to open it off the server's hard disk. This file will probably not exist.
When you upload a file from a web page, it will be posted to the server as part of the POST message. You can get the file out of this from the Form collection. You don't need to convert the uploader to the ASP.NET control, or add the
runat="server"
attribute. So long as your post the form, which contains the input element it will be submitted to the server.The file's contents will be stored as a byte array in the form. You can save this byte array as a file to the server's hard disk somewhere.
In summary:
You don't need to know the path to the file on the client's machine, since you can't access it anyway. Use the file data that is posted as part of the form submit instead to save a copy of the file on the server.