【问题标题】:Downloading file from a server using C# code使用 C# 代码从服务器下载文件
【发布时间】:2015-01-11 19:01:39
【问题描述】:

我有以下代码从服务器下载文件:

private void DocumentDownloadProcess(ProjectDocument projectDocument)
    {

        int projectDocumentId = projectDocument.ProjectDocumentId;
        ProjectDocumentBizManager projectDocumentBM = new ProjectDocumentBizManager();

        projectDocument = projectDocumentBM.GetProjectDocumentById(projectDocumentId);

        int serverId = projectDocument.ServerId;
        //int serverId = 14;
        //ServerBizManager serverBM = BizManagerFactory.BizManagerFactory.GetBizManager<ServerBizManager>();
        //Server server = serverBM.GetServerByServerId(serverId);

        ArchiveServerBizManager serverBM = BizManagerFactory.BizManagerFactory.GetBizManager<ArchiveServerBizManager>();
        ArchiveServer server = serverBM.GetArchiveServerByArchiveServerId(serverId);
        string serverName = server.ServerName;

        string uploadFolder = projectDocument.UploadFolder;

        int loginCredentialId = projectDocument.LoginCredentialId;
        LoginCredentialBizManager loginCBM = BizManagerFactory.BizManagerFactory.GetBizManager<LoginCredentialBizManager>();
        LoginCredential loginCre = loginCBM.GetLoginCredential(loginCredentialId);

        IRemoteServer remoteServer = null;
        remoteServer = RemoteServerFactory.GetRemoteServer(0, serverName, Enums.TransferMethod.COPY, loginCre.UserName, loginCre.EncryptedPassword, false);
        remoteServer.Connect();

        string projectId = Request.Params["ProjectId"];
        string filePath = @"\\" + serverName + @"\" + uploadFolder + @"\" + projectId + @"\" + projectDocument.URL;

        MemoryStream ms = remoteServer.ReadFileToMemoryStream(filePath);

        long dataLengthToRead = ms.Length;
        int blockSize = (int)dataLengthToRead;
        byte[] buffer = new byte[dataLengthToRead];

        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.BufferOutput = true;
        Response.AddHeader("Content-Disposition", "attachment; filename=" + projectDocument.URL);
        Response.AddHeader("Content-Length", ms.Length.ToString());

        while (dataLengthToRead > 0 && Response.IsClientConnected)
        {
            Int32 lengthRead = ms.Read(buffer, 0, blockSize);
            Response.OutputStream.Write(buffer, 0, lengthRead);
            dataLengthToRead = dataLengthToRead - lengthRead;
        }

        Response.Flush();
        Response.Close();
    }

当我在本地运行此代码时,它正在下载文件而没有任何冲突。一旦在任何服务器中部署它,那么如果我尝试下载文件,它会删除几个字节以防文本文件。除文本文件外,所有其他类型的文件都已损坏。

这可能是什么原因?我怎样才能解决这个问题?有没有其他最好的方法来下载文件以免损坏?

【问题讨论】:

    标签: c#


    【解决方案1】:

    很可能是因为 HttpResponse 认为您要输出文本,而不是二进制。

    选项:

    • Response.ContentType 设置为正确的类型
    • 使用Repsonse.BinaryWrite()
    • 只需使用Response.TransmitFile()

    【讨论】:

      【解决方案2】:

      您是否尝试使用IHttpHandler 下载文件?

      ASP.NET file download from server

      【讨论】:

        【解决方案3】:

        我怀疑您在 .aspx 文件中执行此操作。如果是这种情况,请考虑using a Handler。这是an example

        【讨论】:

          猜你喜欢
          • 2012-04-22
          • 1970-01-01
          • 1970-01-01
          • 2020-07-02
          • 1970-01-01
          • 1970-01-01
          • 2015-02-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多