【问题标题】:Download files from the download server with the help of php and mvc借助 php 和 mvc 从下载服务器下载文件
【发布时间】:2019-03-23 08:44:07
【问题描述】:

我有一个文件下载服务器。 我希望我的用户从下载服务器获取文件。 我的下载服务器是Linux。 我想要当用户点击下载按钮时。 直接从下载服务器获取文件。 我不想使用流下载... 我想通过链接连接到下载服务器,然后使用 PHP 下载 我的网站是用 mvc 的

谢谢你,一步一步来帮助我

【问题讨论】:

  • 能否提供您目前拥有的代码?你为完成任务做了什么?
  • @Alexander 实际上他发布了他的代码作为新答案

标签: php asp.net-mvc wcf download


【解决方案1】:

谢谢你

流流 = null;

            //This controls how many bytes to read at a time and send to the client
            int bytesToRead = 10000;

            // Buffer to read bytes in chunk size specified above
            byte[] buffer = new Byte[bytesToRead];

            // The number of bytes read
            try
            {

                //Create a WebRequest to get the file
                HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(Global.UrlVideoPrice + IdCourse + "//" + IdTopic+".rar");




         //Create a response for this request

                HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();

                if (fileReq.ContentLength > 0)
                    fileResp.ContentLength = fileReq.ContentLength;

                //Get the Stream returned from the response
                stream = fileResp.GetResponseStream();

                // prepare the response to the client. resp is the client Response
                var resp = System.Web.HttpContext.Current.Response;

                //Indicate the type of data being sent
                resp.ContentType = "application/octet-stream";

//为文件命名 resp.AddHeader("Content-Disposition", "attachment; filename=\"" + Topic.fldName + ".rar\""); resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

                int length;
                do
                {
                    // Verify that the client is connected.
                    if (resp.IsClientConnected)
                    {
                        // Read data into the buffer.
                        length = stream.Read(buffer, 0, bytesToRead);

                        // and write it out to the response's output stream
                        resp.OutputStream.Write(buffer, 0, length);

                        // Flush the data
                        resp.Flush();

                        //Clear the buffer
                        buffer = new Byte[bytesToRead];
                    }
                    else
                    {
                        // cancel the download if client has disconnected
                        length = -1;
                    }
                } while (length > 0); //Repeat until no data is read
            }
            finally
            {
                if (stream != null)
                {
                    //Close the input stream
                    stream.Close();
                }
            }

这是我的下载代码。 现在我希望用户直接从下载服务器获取文件。

不要使用网站流量来下载文件。 并使用服务器下载流量

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多