/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void _grdDocFiles_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "select") { GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); if (row.Cells[0].FindControl("_linkEdit") != null) { int rowId = ((GridViewRow)((LinkButton)(e.CommandSource)).NamingContainer).RowIndex; int docId = Convert.ToInt32(_grdDocFiles.DataKeys[rowId].Values["Id"].ToString()); Session[SessionName.DocumentId] = docId; Response.Redirect("~/DocumentManagement/ImportDocument.aspx", true); } } else if (e.CommandName == "reupload") { GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); if (row.Cells[0].FindControl("_linkReupload") != null) { int rowId = ((GridViewRow)((LinkButton)(e.CommandSource)).NamingContainer).RowIndex; int docId = Convert.ToInt32(_grdDocFiles.DataKeys[rowId].Values["Id"].ToString()); Session[SessionName.DocumentId] = docId; Response.Redirect("~/DocumentManagement/ReuploadDocument.aspx", true); } } else if (e.CommandName == "fileDownload") { GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); if (row.Cells[0].FindControl("_linkFileDownload") != null) { int rowId = ((GridViewRow)((LinkButton)(e.CommandSource)).NamingContainer).RowIndex; int docId = Convert.ToInt32(_grdDocFiles.DataKeys[rowId].Values["Id"].ToString()); DocumentServiceClient _documentServiceClient = new DocumentServiceClient(); try { Guid ProjectId = DataConstants.DummyGuid; StartDocumentDownloadReturnValue Header; if (!string.IsNullOrEmpty(Convert.ToString(Session[SessionName.ProjectId]))) { ProjectId = new Guid(Convert.ToString(Session[SessionName.ProjectId])); } #region Start Download try { Header = _documentServiceClient.StartDocumentDownloadForMatter(((LogonReturnValue)Session[SessionName.LogonSettings]).LogonId, ProjectId, docId); } catch (Exception ex) { throw ex; } #endregion if (Header != null) { if (!Header.Success) { _lblError.CssClass = "errorMessage"; _lblError.Text = Header.Message; return; } if (!Directory.Exists(Path.Combine(Server.MapPath("."), "Download"))) { Directory.CreateDirectory(Path.Combine(Server.MapPath("."), "Download")); } long BytesLeft = Header.Size; string downloadFolderPath = Path.Combine(Server.MapPath("."), "Download"); string FileName = Path.Combine(downloadFolderPath, Header.FileName); try { #region Download Chunk by Chunk // Open the file using (FileStream FileStream = new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.Read)) { while (BytesLeft > 0) { // Download a chunk DownloadChunkReturnValue Chunk = _documentServiceClient.DocumentDownloadChunk( ((LogonReturnValue)Session[SessionName.LogonSettings]).LogonId, Header.TransferId, FileStream.Position); if (!Chunk.Success) { throw new Exception("File download failed: " + Chunk.Message); } // Write the chunk to the file FileStream.Write(Chunk.Bytes, 0, Chunk.ChunkSize); BytesLeft -= Chunk.ChunkSize; } } #endregion #region Download Complete // Tell the service we have finished downloading _documentServiceClient.DocumentDownloadComplete(((LogonReturnValue)Session[SessionName.LogonSettings]).LogonId, Header.TransferId); #endregion // Check the file size if (new FileInfo(FileName).Length != Header.Size) { throw new Exception("File download failed, file size is wrong"); } // Make a hash of the file byte[] LocalHash = FileTransferHash.CreateFileMD5Hash(FileName); // Check the hash matches that from the server if (!FileTransferHash.CheckHash(LocalHash, Header.Hash)) { throw new Exception("File download failed, document checksum does not match"); } // Set the file modified date File.SetLastWriteTime(FileName, Header.ModifiedDate); try { //string jscript = "HideProgress();window.open('DocumentView.aspx?file={0}&path={1}','FileDownload','height=200,width=400,status=no,toolbar=no,menubar=no,left=0,top=0');"; string jscript = "HideProgress(); GetDocument('" + AppFunctions.GetASBaseUrl() + "/Pages/DocMgmt/DocumentView.aspx?file={0}&path={1}');"; ScriptManager.RegisterStartupScript(this.Page, this.GetType(), Guid.NewGuid().ToString(), string.Format(jscript, HttpUtility.UrlEncode(new FileInfo(FileName).Name), HttpUtility.UrlEncode(FileName)), true); } catch (Exception ex) { throw ex; } } catch { // Download failed so delete file being downloaded File.Delete(FileName); throw; } } } catch (System.ServiceModel.EndpointNotFoundException) { _lblError.Text = DataConstants.WSEndPointErrorMessage; _lblError.CssClass = "errorMessage"; } catch (Exception ex) { _lblError.CssClass = "errorMessage"; _lblError.Text = ex.Message; } finally { if (_documentServiceClient != null) { if (_documentServiceClient.State != System.ServiceModel.CommunicationState.Faulted) { _documentServiceClient.Close(); } } } } } }