1
Download request refers to an invalid range
Question asked by Mikel dilas - 9/19/2018 at 8:52 PM
Answered
Hi,
 
I tried to retrieve data from sql server, and I followed the steps on the web, but an error appears "Download request to refresh invalid range" ver 4.2
 
 
 
  protected void Page_Load(object sender, EventArgs e)
        {
            documentViewer.DocumentHandlerType = typeof(DbDocumentHandler);
            documentViewer.Document = "1"; // a file path or identifier
            //documentViewer.DocumentSource = new DocumentSource(
            //    new DocumentInfo("1", "filename"),
            //    new StreamResult(stream)
            //);
        }





public class DbDocumentHandler : IDocumentHandler
    {
        string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        // 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>(strConnString);

            using (var connection = new SqlConnection(strConnString))
            {
                connection.Open();

                var sql = "SELECT Name FROM Files WHERE Id=" + 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
            );
        }

        // 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>(strConnString);

            using (var connection = new SqlConnection(strConnString))
            {
                connection.Open();

                var sql = "SELECT Data FROM Files WHERE Id=" + 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 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);
        }
    }
 

1 Reply

Reply to Thread
0
Cem Alacayir Replied
Employee Post Marked As Answer
This error would happen if you provide the same uniqueID for 2 different files:
 
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.
 
So your first provided this ID for a stream and that document was cached:
documentViewer.Document = "1"; // a file path or identifier
but then you provided the same ID for a different stream so a cache collision occurs. To reset it, you can clear your browser's cache (the error is due to a file size mismatch between client-side browser cache and server-side DocumentCache folder).
 

Reply to Thread