无法从服务器获取完整映像

本文关键字:映像 获取 服务器 | 更新日期: 2023-09-27 18:25:09

我有一个C#窗体应用程序,它从url下载文件(asp.net应用程序),但它没有返回完整的图像,比如说图像是780kb,windows窗体创建的文件正好是381字节。

我搞不清楚这个问题。请帮忙。

我用来下载的代码是:

 public bool getFileFromURL(string url, string filename)
        {
            long contentLength = 0;
            Stream stream = null;
            try
            {
                WebRequest req = WebRequest.Create(url);
                WebResponse response = req.GetResponse();
                stream = response.GetResponseStream();
                contentLength = response.ContentLength;

                // Transfer the file
                byte[] buffer = new byte[10 * 1024]; // 50KB at a time
                int numBytesRead = 0;
                long totalBytesRead = 0;
                using (FileStream fileStream = new FileStream(filename, FileMode.Create))
                {
                    using (BinaryWriter fileWriter = new BinaryWriter(fileStream))
                    {
                        while (stream.CanRead)
                        {
                            numBytesRead = stream.Read(buffer, 0, buffer.Length);
                            if (numBytesRead == 0) break;
                            totalBytesRead += numBytesRead;
                            fileWriter.Write(buffer, 0, numBytesRead);
                        }
                        fileWriter.Close();
                    }
                    fileStream.Close();
                }
                stream.Close();
                response.Close();
                req.Abort();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

这是我的asp.net应用程序代码:

   using (PortalEntities db = new PortalEntities())
        {
                PortalModel.Command command = db.Commands.SingleOrDefault(c => c.id == id);
                var filePath = Server.MapPath("~/Uploads/"+command.arguments);
                if (!File.Exists(filePath))
                    return;
                var fileInfo = new System.IO.FileInfo(filePath);
                Response.ContentType = "image/jpg";
                Response.AddHeader("Content-Disposition", String.Format("attachment;filename='"{0}'"", filePath));
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.WriteFile(filePath);
                Response.End();
        }

无法从服务器获取完整映像

从web响应向文件中写入一些字节需要大量的代码。像这样的东西怎么样(.NET4+):

public static bool GetFileFromURL(string url, string filename)
{
    try
    {
        var req = WebRequest.Create(url);
        using (Stream output = File.OpenWrite(filename))
        using (WebResponse res = req.GetResponse())
        using (Stream s = res.GetResponseStream())
            s.CopyTo(output);
        return true;
    }
    catch
    {
        return false;
    }
}

您可以以更优雅的方式下载图像,前面已经讨论过了。无法在图像类中定位FromStream

并使用File.WriteAllBytes方法将字节数组保存为文件,更多信息请访问http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes(v=vs.110).aspx

所以你所有的客户端代码都可以用代替

public void getFileFromURL(string url, string filename)
{
    using (var webClient = new WebClient())
    {
        File.WriteAllBytes(filename,webClient.DownloadData(url));
    }
}

老兄,你为什么不使用WebClient.DownloadFileAsync?

private void DownloadFile(string url, string path)
{
    using (var client = new System.Net.WebClient())
    {
        client.DownloadFileAsync(new Uri(url), path);
    }
}

差不多就是这样,但这种方法不能下载超过2GB的内容。但我不认为图像是那么大的xD。

希望它能有所帮助!