0

I have the following code to download a file from a server:

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();
    }

when I run this code in my local it is downloading the file without any conflict. Once deployed this in any server then if I tried to download a file it is deleting few bytes in case of text file. other than text file all the other types of files are getting corrupted.

what may be the cause for this? how can I fix this? Is there any other best way to download file so that it could not be corrupted?

kartikasi
  • 29
  • 2
  • 9

3 Answers3

0

Most likely because the HttpResponse thinks you want to output text, not binary.

Options:

  • Set the Response.ContentType to the proper type
  • Use Repsonse.BinaryWrite()
  • Just use Response.TransmitFile()
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

Did you try using the IHttpHandler to download the file?

ASP.NET file download from server

Community
  • 1
  • 1
Kulasangar
  • 9,046
  • 5
  • 51
  • 82
0

I'm suspecting you're doing this in an .aspx file. If that's the case, consider using a Handler. Here's an example.

BCdotWEB
  • 1,009
  • 1
  • 14
  • 35