FileWebRequest不返回它应该 [英] FileWebRequest not returning what it should

查看:113
本文介绍了FileWebRequest不返回它应该的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的机器发送文件给浏览器,以便它可以下载,在.NET应用程序我的工作。我现在用的是code在,以便回答,而是用一个HttpWebRequest的我使用的是FileWebRequest因为我访问我的本地计算机上的文件。请求是这样的: FileWebRequest fileReq =(FileWebRequest)WebRequest.Create(@文件:/// C:/Tmp/new.html); ,当我复制网址文件:/// C:/Tmp/new.html 到浏览器中,它给了我正确的文件。但是,当我使用 fileReq.Co​​ntentLength 在我的code,它总是返回 0 ,这使我相信,该文件没有被读取的某些原因。谁能告诉我这是怎么回事?

编辑:这是我的code,就像我说的完全一样,从其他太问题,但我用FileWebRequest,而不是HttpWebRequest的

 流流= NULL;
        INT bytesToRead = 10000;
        byte []的缓冲区=新的字节[bytesToRead]
        尝试
        {
            FileWebRequest fileReq =(FileWebRequest)WebRequest.Create(@文件:/// C:/Tmp/new.html);
            FileWebResponse fileResp =(FileWebResponse)fileReq.GetResponse();

            如果(fileReq.Co​​ntentLength大于0)
            {
                fileResp.ContentLength = fileReq.Co​​ntentLength;
                流= fileResp.GetResponseStream();
                VAR RESP = HttpContext.Current.Response;
                resp.ContentType =应用程序/八位字节流;
                resp.AddHeader(内容dsiposition,附件;文件名=+网址);
                resp.AddHeader(内容长度,fileResp.ContentLength.ToString());

                INT长;
                做
                {
                    如果(resp.IsClientConnected)
                    {
                        长度= stream.Read(缓冲液,0,bytesToRead);
                        resp.OutputStream.Write(缓冲液,0,长度);
                        resp.Flush();
                        缓冲区=新的字节[bytesToRead]
                    }
                    其他
                    {
                        长度= -1;
                    }
                }而(长度大于0);
            }
        }
        赶上(例外前)
        {
            FileLabel.Text = ex.Message.ToString();
        }
        最后
        {
            如果(流!= NULL)
            {
                stream.Close();
            }
        }
 

解决方案

从这里开始:的 http://msdn.microsoft.com/en-us/library/12s31dhy(V = vs.110)的.aspx 这种方法只接受的文件路径,并没有休息为您服务。

I am trying to send a file from my machine to the browser, so that it can be downloaded, in a .NET application I am working on. I am using the code in the this SO answer, but instead of using an HttpWebRequest I am using a FileWebRequest because I am accessing the file on my local machine. The request looks like this: FileWebRequest fileReq = (FileWebRequest)WebRequest.Create(@"file:///C:/Tmp/new.html"); and when I copy the url file:///C:/Tmp/new.html into the browser, it gives me the correct file. But when I use fileReq.ContentLength in my code it always returns 0, which leads me to believe that the file is not being read for some reason. Can anyone tell me what's going on here?

EDIT: Here's my code, like I said exactly like from the other SO question, but I used FileWebRequest instead of HttpWebRequest.

        Stream stream = null;
        int bytesToRead = 10000;
        byte[] buffer = new Byte[bytesToRead];
        try
        {               
            FileWebRequest fileReq = (FileWebRequest)WebRequest.Create(@"file:///C:/Tmp/new.html");
            FileWebResponse fileResp = (FileWebResponse)fileReq.GetResponse();

            if (fileReq.ContentLength > 0)
            {
                fileResp.ContentLength = fileReq.ContentLength;
                stream = fileResp.GetResponseStream();
                var resp = HttpContext.Current.Response;
                resp.ContentType = "application/octet-stream";
                resp.AddHeader("Content-dsiposition", "attachment; filename=" + url);
                resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

                int length;
                do
                {
                    if (resp.IsClientConnected)
                    {
                        length = stream.Read(buffer, 0, bytesToRead);
                        resp.OutputStream.Write(buffer, 0, length);
                        resp.Flush();
                        buffer = new Byte[bytesToRead];
                    }
                    else
                    {
                        length = -1;
                    }
                } while (length > 0);
            }
        }
        catch (Exception ex)
        {
            FileLabel.Text = ex.Message.ToString();
        }
        finally
        {
            if (stream != null)
            {
                stream.Close();
            }
        }

解决方案

Start here: http://msdn.microsoft.com/en-us/library/12s31dhy(v=vs.110).aspx This method just accepts the file path and does the rest for you.

这篇关于FileWebRequest不返回它应该的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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