// Open a readable stream for the current input file. // This is called only when necessary, i.e first time the document is loaded. For consecutive views // as long as cached files are valid, it will not be called. This can be also called when "Download" // button is clicked to download the original document. // // inputFile parameter will be the value that was set in DocumentViewer.Document property, i.e. // the input file that was requested to be loaded in DocumentViewer // // inputOptions parameter will be determined according to the input document format // Usually you will not need to check this parameter as inputFile parameter should be sufficient // for you to locate and open a corresponding stream. // // Return a StreamResult instance initialized with a readable System.IO.Stream object. public StreamResult OpenRead(string inputFile, InputOptions inputOptions, DocumentHandlerParameters handlerParameters) { var fileId = inputFile; byte[] fileBytes; // Get your parameters that were set in documentViewer.DocumentHandlerParameters property // The type for the generic Get<T> method should be the same as the set value's type. var connectionString = handlerParameters.Get <string>("connectionString"); using (var connection = new SqlConnection(connectionString)) { connection.Open(); var sql = "SELECT FileBytes FROM FileTable WHERE FileId=" + fileId; using (var command = new SqlCommand(sql)) using (var reader = command.ExecuteReader()) { if (!reader.Read()) { throw new Exception("File not found"); } // read the file data from the selected row (first column in above query) fileBytes = (byte[])reader.GetValue(0); } } // We need to return a stream that has the file contents here. // As we don't have a stream but a byte array, we can wrap it with a MemoryStream. var stream = new MemoryStream(fileBytes); return(new StreamResult(stream)); }
// Get the document information required for the current input file. // This is called for determining the cache key and document format whenever // DocumentViewer requests a document. // // inputFile parameter will be the value that was set in DocumentViewer.Document property, i.e. // the input file that was requested to be loaded in DocumentViewer // // Return a DocumentInfo instance initialized with required information from this method. public DocumentInfo GetInfo(string inputFile, DocumentHandlerParameters handlerParameters) { var fileId = inputFile; string fileName; // Get your parameters that were set in documentViewer.DocumentHandlerParameters property // The type for the generic Get<T> method should be the same as the set value's type. var connectionString = handlerParameters.Get <string>("connectionString"); using (var connection = new SqlConnection(connectionString)) { connection.Open(); var sql = "SELECT FileName FROM FileTable WHERE FileId=" + fileId; using (var command = new SqlCommand(sql, connection)) using (var reader = command.ExecuteReader()) { if (!reader.Read()) { throw new Exception("File not found"); } // read the file name from the selected row (first column in above query) fileName = reader.GetString(0); } } return(new DocumentInfo( // uniqueId parameter (required): // The unique identifier that will be used for generating the cache key for this document. // For instance, it can be an ID from your database table or a simple file name; // you just need to make sure this ID varies for each different document so that they are cached correctly. // For example for files on disk, // we internally use a string combination of file extension, file size and file date for uniquely // identifying them, this way cache collisions do not occur and we can resuse the cached file // even if the file name before extension is changed (because it's still the same document). fileId, // fileName parameter (optional but recommended): // The file name which will be used for display purposes such as when downloading the document // within DocumentViewer> or for the subfolder name prefix in cache folder. // It will also be used to determine the document format from extension if format // parameter is not specified. If not specified or empty, uniqueId will be used // as the file name. fileName )); }