怎么用vb.net实现下载文件,并且知道文件名

时间:2021-12-25 14:02:52


我现在用
 Dim documentURL
 documentURL = "http://mytest.net/filetran.jsp?u=100001&p=12345&i=0123456789abcdef&c=20"
 Dim wc As New System.Net.WebClient
 wc.DownloadFile(documentURL, dlFileName)

来做的话,就必须指定 dlFileName 的文件名

因为在服务器上有各种各样的文件(如 .doc .ppt .pdf etc)我用什么办法才可以实现动态获得文件名呢?


6 个解决方案

#1


HttpWebRequest hwrq; 
   HttpWebResponse hwrp = null; 
    hwrq = (HttpWebRequest) WebRequest.Create(url); 
    hwrp = (HttpWebResponse) hwrq.GetResponse(); 
    a = hwrp.Headers["Content-Disposition"]; //attachment 
    if (a != null) 
    { 
     a = a.Substring(a.LastIndexOf("filename=") + 9); 
    } 


  
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 

最新版本:20070212

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html

#2


小弟初学编程,对各种语言还不是很了解,有没有vb.net的实现方法?

#3


Dim hwrq As HttpWebRequest 
Dim hwrp As HttpWebResponse = Nothing 
hwrq = CType(WebRequest.Create(url), HttpWebRequest) 
hwrp = CType(hwrq.GetResponse, HttpWebResponse) 
dim a as string
a = hwrp.Headers("Content-Disposition") 
If Not (a Is Nothing) Then 
 a = a.Substring(a.LastIndexOf("filename=") + 9) 
End If

#4


sorry , 怪我没有把想说的整理清楚。
重新说明一下

到目前为止,我用下面的做法,实现了从服务器那里download到文件并指定保存为dlFileName的文件,如下

 Dim  documentURL
 Dim dlFileName
 documentURL  =  "http://mytest.net/filetran.jsp?u=100001&p=12345&i=0123456789abcdef&c=20"  
 dlFileName = "c:/aa.pdf"
 Dim  wc  As  New  System.Net.WebClient  
 wc.DownloadFile(documentURL,  dlFileName)  

但是,现在的问题是,服务器端各种文件的后缀不一样,如.doc .xls 等等。显然,用我现在的方法,下载下来的文件都被保存为 aa.pdf 了。

我想做的是 有没有办法把下下来的文件保存为持有正确后缀名的文件

在此先感谢 feiyu0112之前的2次回答 先给10分

#5


feiyu0112 大大,因为看不到给分密码,暂时不能给你分,不好意思

#6


感谢 feiyu0112 ,按你给的方法我取到了,感谢!

#1


HttpWebRequest hwrq; 
   HttpWebResponse hwrp = null; 
    hwrq = (HttpWebRequest) WebRequest.Create(url); 
    hwrp = (HttpWebResponse) hwrq.GetResponse(); 
    a = hwrp.Headers["Content-Disposition"]; //attachment 
    if (a != null) 
    { 
     a = a.Substring(a.LastIndexOf("filename=") + 9); 
    } 


  
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 

最新版本:20070212

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html

#2


小弟初学编程,对各种语言还不是很了解,有没有vb.net的实现方法?

#3


Dim hwrq As HttpWebRequest 
Dim hwrp As HttpWebResponse = Nothing 
hwrq = CType(WebRequest.Create(url), HttpWebRequest) 
hwrp = CType(hwrq.GetResponse, HttpWebResponse) 
dim a as string
a = hwrp.Headers("Content-Disposition") 
If Not (a Is Nothing) Then 
 a = a.Substring(a.LastIndexOf("filename=") + 9) 
End If

#4


sorry , 怪我没有把想说的整理清楚。
重新说明一下

到目前为止,我用下面的做法,实现了从服务器那里download到文件并指定保存为dlFileName的文件,如下

 Dim  documentURL
 Dim dlFileName
 documentURL  =  "http://mytest.net/filetran.jsp?u=100001&p=12345&i=0123456789abcdef&c=20"  
 dlFileName = "c:/aa.pdf"
 Dim  wc  As  New  System.Net.WebClient  
 wc.DownloadFile(documentURL,  dlFileName)  

但是,现在的问题是,服务器端各种文件的后缀不一样,如.doc .xls 等等。显然,用我现在的方法,下载下来的文件都被保存为 aa.pdf 了。

我想做的是 有没有办法把下下来的文件保存为持有正确后缀名的文件

在此先感谢 feiyu0112之前的2次回答 先给10分

#5


feiyu0112 大大,因为看不到给分密码,暂时不能给你分,不好意思

#6


感谢 feiyu0112 ,按你给的方法我取到了,感谢!