简体   繁体   中英

Cannot implicitly convert type 'System.IO.Stream' to 'Stream'

I'm using the following code in order to stream a video on the aspx page and showing to the user.

But I get the following error in running the code:

CS0029: Cannot implicitly convert type 'System.IO.Stream' to 'Stream'

May you please help me whats wrong with this code?

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web;

public partial class Stream : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Stream stream = null;
        int bytesToRead = 10000;
        byte[] buffer = new Byte[bytesToRead];
        try
        {
            HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create("the url");
            HttpWebResponse fileResp = (HttpWebResponse)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-Disposition", "attachment; filename=\"" + "ali" + "\"");
            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);
        }
        finally
        {
            if (stream != null)
            {
                stream.Close();
            }
        }

    }
}

stream = fileResp.GetResponseStream(); is wrong

The declare of the function as follow public override Stream GetResponseStream()

https://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream(v=vs.110).aspx

you can change the code to (NetworkStream) fileResp.GetResponseStream();

To explain it in more detail, the Stream is an abstract class, it should initialize from its child class, in your case, you give an abstract object with abstract class type which violate this rule.

Your class name is the same as the type returned by .GetResponseStream() . When you do Stream stream it defines stream as an object or your class not of System.IO.Stream . You have a couple of options. One is you can declare it as following:

System.IO.Stream stream = null;

Another is define your own for it as a using statement outside the namespace

using IOStream = System.IO.Stream;

Then you can use IOStream everywhere you need like this:

IOStream stream = null;
stream = fileResp.GetResponseStream(); 

This line is throwing compilation error I believe. Your stream referance is of type Class stream . Please use

System.IO.Stream stream = null; 

while initializing.. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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