简体   繁体   English

C#通过url id下载docx文件(无内容处理)

[英]C# Download a docx file via url id (no content-disposition)

I've spent about 3-4 days on investigating this via stack overflow and google search engine. 我花了大约3-4天的时间通过堆栈溢出和谷歌搜索引擎来调查这个问题。

My dilemma is this: I am tasked with displaying a file from a url (not sure if i can simply open it or have to save it to a folder) within a windows form on Visual Studio 2015. The link in question is this but will not work due to this being only accessible via internal network or VPN connected: http://QualityWorkbench/ivscripts/qwbcgi.dll/docfetch?db=live&id=1090 我的困境是这样的:我的任务是在Visual Studio 2015上的Windows窗体中显示来自URL的文件(不确定我是否可以简单地打开它或必须将其保存到文件夹中)。有问题的链接是这样但是会由于只能通过内部网络或连接的VPN访问,因此无法正常工作: http://QualityWorkbench/ivscripts/qwbcgi.dll/docfetch?db = live&id = 1090

What I've accomplished so far is that that I've built the windows form portion based on this link from a stack overflow article which leads to a blog link: 到目前为止我所完成的是,我已经根据此链接从堆栈溢出文章构建了Windows窗体部分,该文章引出了一个博客链接:

How to display PDF or Word's DOC/DOCX inside WinForms window? 如何在WinForms窗口中显示PDF或Word的DOC / DOCX?

What I've built very similarly would work for links that have a file name within the url but not the Quality Workbench link mentioned above. 我非常类似地构建的内容适用于在url中具有文件名但不是上面提到的Quality Workbench链接的链接。 An example is this: http://www.e-iceblue.com/images/test.docx 一个例子是: http//www.e-iceblue.com/images/test.docx

I've researched everywhere and various stack overflow articles talk about checking content disposition via HttpWebResponse headers but when i debug through it, the only headers i saw were these (nothing about content-disposition): 我已经到处研究了各种堆栈溢出文章,讨论通过HttpWebResponse标题检查内容处理,但是当我通过它进行调试时,我看到的唯一标题是这些(没有关于内容处置):

Header Name:Content-Length, Value :411 标题名称:内容长度,值:411

Header Name:Content-Type, Value :text/html 标题名称:内容类型,值:text / html

Header Name:Set-Cookie, Value :IGS_SessionID=53114; 标题名称:Set-Cookie,值:IGS_SessionID = 53114; path=/; 路径= /; expires=Fri, 03 Jun 2016 14:01:34 GMT,sessionid=2d794a41-43ce-4c6b-96cc-7a832ac39670; expires = Fri,2016年6月3日14:01:34 GMT,sessionid = 2d794a41-43ce-4c6b-96cc-7a832ac39670; path=/; 路径= /; expires=Fri, 03 Jun 2016 14:01:34 GMT expires = Fri,2016年6月3日14:01:34 GMT

Header Name:Server, Value :Microsoft-IIS/7.5 标题名称:服务器,值:Microsoft-IIS / 7.5

Header Name:X-Powered-By, Value :ASP.NET 标题名称:X-Powered-By,值:ASP.NET

I know what I've listed is pretty vague so I'll be happy to provide any coding details or whatever info anyone needs to decipher my dilemma: how to download a file from a url that only ends with an id using Visual Studio 2015 C#. 我知道我列出的内容非常模糊,所以我很乐意提供任何编码细节或任何人解读我的困境所需的信息:如何从一个只使用Visual Studio 2015 C#结尾的URL下载文件# 。 Ideally opening the file without saving it and display it into a window form is my ideal but I am also content in saving the file into a users download folder. 理想情况下打开文件而不保存它并将其显示为窗口形式是我的理想选择,但我也很满意将文件保存到用户下载文件夹中。

First of all, you need to make sure the URL you use really provides you a file; 首先,您需要确保您使用的URL确实为您提供了一个文件; as Hintham suggested in his/hers comment, what you receiving is not a docx file. 正如Hintham在他/她的评论中所说,你收到的不是docx文件。

Second, in your code you need to make a HTTP request to the URL to get the file. 其次,在您的代码中,您需要向URL发出HTTP请求以获取文件。 Good practice would be to catch transmission and/or HTTP errors. 好的做法是捕获传输和/或HTTP错误。 In the response there will be the file if no HTTP errors has occurred during requesting. 如果在请求期间没有发生HTTP错误,则在响应中将存在该文件。

The easiest way to do this (in my opinion) would be to engage the WebClient.DownloadData . (在我看来)最简单的方法是使用WebClient.DownloadData It will retrieve the file into a byte array so you could fed your WinForms app with it for display. 它会将文件检索到一个字节数组中,以便您可以将WinForms应用程序与其一起显示。 Take a look on the MSDN documentation - it's pretty straight forward. 看一下MSDN文档 - 它非常简单。

@bozhidarstoinev - after taking a long break and coming back to it, i reviewed what you provided and then viewed the html source of the link on IE. @bozhidarstoinev - 经过长时间休息并回到它之后,我回顾了你提供的内容,然后查看了IE链接的html源代码。 It was like you said, the link was referencing an html site which contained the real URL download link. 就像你说的那样,该链接引用了一个包含真实URL下载链接的html网站。 I didn't catch it the first time due to fatigue but the true url download link was similarly named to what i provided in my problem save 3 characters added on. 由于疲劳,我没有第一次抓住它,但真正的网址下载链接同样命名为我在我的问题中提供的保存3个字符。

Thanks for setting me on the right path! 谢谢你让我走上了正确的道路!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM