直接将文件下载到文件夹? [英] Download a file directly to a folder?

查看:70
本文介绍了直接将文件下载到文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码可以工作,但它有一个提示,说你想保存文件吗?是否有可能自动下载到特定文件夹?:



I have this code which works but it has a prompt that sayas "Do you want to save the file?" Is it possible that it can be automatically downloded to a specific folder?:

Public Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

    If e.CommandName = "download" Then
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim url As String = "D:\Work\2014\Programs\Trials\TheScripts\" + GridView1.Rows(index).Cells(1).Text
        Dim file As New System.IO.FileInfo(url)
        If file.Exists Then
            Response.Clear()
            Response.AppendHeader("Content-Disposition:", "attachment; filename=" & file.Name)
            Response.AppendHeader("Content-Length", file.Length.ToString())
            Response.ContentType = "application/octet-stream"
                       Response.TransmitFile(Server.MapPath("D:\Work\2014\Programs\Trials\TheScripts\DOWNLOAD\" + GridView1.Rows(index).Cells(2).Text))
            Response.[End]()
        Else
            Response.Write("NO FILE PRESENT")
        End If
    End If
End Sub

< br $>




错误:

System.Web中出现'System.Web.HttpException'类型的异常。 dll但未在用户代码中处理



附加信息:'D:/Work/2014/Programs/Trials/TheScripts/DOWNLOAD/108AB_1D.DAT'是实物路径,但预计会有虚拟路径。




Error:
An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code

Additional information: 'D:/Work/2014/Programs/Trials/TheScripts/DOWNLOAD/108AB_1D.DAT' is a physical path, but a virtual path was expected.

推荐答案

在谈论下载之前,您只需要了解Web的功能。你没有。



首先,不可能以如此肮脏的风格进行编程。看看下载。你在多个地方使用这个立即常数,否则它无法工作。这是不可支持的。不要用字符串匹配任何东西,即使你这样做,也要声明显式常量。



你不能使用 Server.MapPath 那样的。没有固定的硬编码文件名可能有用的情况。此外,Web应用程序在沙盒环境中执行,这使得服务器上的文件系统无法访问。您只能访问在您配置的根目录下的文件(〜/)。所以,你的路径应该是相对于页面,或者是相对于这个根目录。



最后,你要制定的问题。当客户端收到具有该上下文类型的HTTP响应时,典型的Web浏览器将建议用户保存该文件。哪里?用户告诉它保存的位置。相反,你在谈论一个特定文件夹。这表明您正在考虑客户端计算机上的特定文件夹。 但是您的Web应用程序/站点无法访问客户端系统。



请理解:这甚至不是可能或不可能。 这没有任何意义。想象一下这是可能的;并将内容转到文件中,例如C:/myFantasy/SomeCrazyContent.SomeDataFormat。但是这个目录C:/ myFantasy甚至可能不存在;甚至驱动器C:也可能不存在,并且,在许多操作系统上,例如,在非Microsoft OS上,驱动器号的整个概念可能不存在。是否明白为什么它没有意义?



-SA
Before talking about downloads, you just need to understand what Web does. You don't.

First of all, It's impossible to program in such a dirty style. Look at "download". You use this immediate constant in more then one place, otherwise it could not work. This is unsupportable. Don't match anything by strings, and, even if you do, declare explicit constant.

You cannot use Server.MapPath like that. There are no cases when fixed hard-coded file names could be useful. Moreover, Web applications are executed in sandboxed environments which makes the file system on the server inaccessible. You can only access the files under the root directory configured fro your side ("~/"). So, your paths should be relative to the page, or to this root directory.

And finally, to the problem you formulated. When a client received the HTTP response with that context type, a typical Web browser will suggest the user to save the file. Where? Where the user tells it to save. Instead, you are talking about a "specific folder". This makes it apparent that you are thinking about a "specific folder" on the client machine. But the client system is not accessible by your Web application/site.

Please understand: this is not even about "possible" or "impossible". It just makes no sense. Imagine that it was possible; and the content goes to the file, say "C:/myFantasy/SomeCrazyContent.SomeDataFormat". But this directory, "C:/myFantasy" may not even exist; even the drive "C:" may not exist, and, on many OS, for example, on non-Microsoft OS, the whole concept of "drive letter" may not exist. Is it clear no why it makes no sense?

—SA


这篇关于直接将文件下载到文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆