We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18654
    • 191 Posts
    I’m probably missing something really simple, but for some reason I can’t get downloads to work for static resources. Here’s a link to a short 2 minute clip showing the Content Type setup and the settings for the Static Resource itself.

    Note: The url shown in the screencast isn’t available yet publicly, it’s part of a new version of the SoundRezn site which is only viewable if logged into the manager. If posting an accessible URL would be helpful for troubleshooting I can post it to the current site.
      God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
      • 22303 MODX Staff
      • 10,725 Posts
      It looks like you had the mime-type as .audio/mpeg3 instead of just audio/mpeg3, but I could not tell from the screencast. If so, that would be an invalid header. FWIW, you can check the headers of any response using the HttpFox extension for Firefox; I highly recommend it for debugging just about any web site problem. If you compare the responses from the direct download of the mp3 that is working to the one you are getting with the static resource, that might tell you what the problem is.
        • 18654
        • 191 Posts
        OpenGeek - thanks for the response. I went to http://hurl.it/ and compared the response headers from a successful download (using a snippet in a regular document resource) to the response headers using a static resource. Here is what I found (you can paste these urls into hurl.it and see the responses there - the successful download takes a while because it actually downloads the entire file before it shows the headers):

        Successful Download Response Headers from http://soundrezn.com/index.php?id=283&resID=280 (Download Snippet used in a ModDocument Resource):

        HTTP/1.1 200 OK
        Date: Mon, 10 May 2010 09:46:10 GMT
        Server: Apache
        Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
        Content-Disposition: attachment; filename=CountingOnGod.mp3
        Content-Transfer-Encoding: binary
        Expires: Thu, 19 Nov 1981 08:52:00 GMT
        Pragma: no-cache
        X-Powered-By: PHP/5.2.13
        Set-Cookie: PHPSESSID=9281057062b3d7d1e34f6964fb21df67; expires=Mon, 17-May-2010 09:46:10 GMT; path=/
        Content-Length: 5914794
        Content-Type: application/octet-stream
        


        Unsuccessful Download Response Headers from http://soundrezn.com/index.php?id=280 (A ModStaticResource):
        HTTP/1.1 200 OK
        Date: Mon, 10 May 2010 09:52:04 GMT
        Server: Apache
        Cache-Control: public
        Content-Disposition: attachment; filename=281
        Expires: Thu, 19 Nov 1981 08:52:00 GMT
        Pragma: no-cache
        Vary: User-Agent
        X-Powered-By: PHP/5.2.13
        Set-Cookie: PHPSESSID=1d2ac76efdcb06978cba029a05bbcb6d; expires=Mon, 17-May-2010 09:52:05 GMT; path=/
        Transfer-Encoding: chunked
        Content-Type: application/octet-stream
        


        PHP Code for successful Download
        <?php
        	if(isset($_GET['resID'])){		
        
        		$resourceID = $_GET['resID'];
        		$staticResource = $modx->getObject('modStaticResource', array('id' => $resourceID));
        		$fileLocation = '';
        		
        		if ($staticResource) {
        			$fileLocation = $staticResource->get('content');
        		}
        		
        		$fileParts = pathinfo($fileLocation);
        		$fileExtension = strtoupper($fileParts['extension']);
        		$filename = $fileParts['filename'] . '.' . $fileParts['extension'];
        		$filepath = '/path/to/server/root'.$fileLocation;		
        		$contentDisposition = $fileExtension == 'PDF' ? 'inline' : 'attachment';
        		$applicationType = $fileExtension == 'PDF' ? 'application/pdf' : 'application/octet-stream';	
        		
        		// check that it exists and is readable
        		if (file_exists($filepath) && is_readable($filepath)) {
        
        			$size = filesize($filepath);
        			if($size > 31457280) {
        				header("location: http://www.soundrezn.com/assets/php/functions/download.php?fileLocation=$fileLocation");
        				exit;
        			}
        			else {
        				header('Content-Type: ' . $applicationType);
        				header('Content-Length: ' . $size);
        				//header('Content-Disposition: inline; filename=' .$filename);
        				header('Content-Disposition: ' .$contentDisposition . '; filename=' .$filename);
        				header('Content-Transfer-Encoding: binary');
        				// open the file in binary read-only mode
        				$file = fopen($filepath, 'rb');
        				
        				if ($file) {
        					// stream the file and exit the script when complete
        					fpassthru($file);
        					exit;
        				}
        			}
        		}
        	}
        ?>
        


        Any ideas on what I could be doing wrong? I noticed the header response says "chunked" instead of binary even though I have "binary" checked - not sure if this is correct. Also, in the DB, I saw a field for "headers" in the modx_content_type table. Is this field accessible somewhere in the GUI?

        BTW - did you view the screencast in HD/Full view? You had mentioned that you weren’t able to see if there was a "." before "audio/mpeg" (there wasn’t) - it looked pretty clear to me when I viewed the video full screen, so I was just curious about that.

        Any suggestions or troubleshooting ideas would be appreciated!

        Thanks,

        -matt
          God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
          • 18654
          • 191 Posts
          Any ideas on how I might further troubleshoot this issue? Does anyone currently have Static Resources working correctly (i.e. can use it to download .mp3, .pdf or .flv) for rev6748 or above?

          -matt
            God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
            • 22303 MODX Staff
            • 10,725 Posts
            All the downloads on the MODx CMS site work fine. This appears to be related specifically to the needs of an mp3 file or something. I’ll try and do some testing of this soon myself to see if I can figure anything out...
              • 18654
              • 191 Posts
              I’ve tried PDF files as well - and no luck there either. However, I was able to successfully download both .mp3’s and .pdf’s on my localhost with RC1-rev6457. Using the exact same environment (localhost / wamp 2.0 / W7) with RC2-rev6672 I was unable to successfully download anything. Something else I noticed is that, even with the version that downloaded successfully (rev6457), the file extension is not added to .mp3 files whereas it is added to pdf files. So, with rev6457, it will successfully download an mp3 file, but the name of the file ends up being 12 (resource id) instead of 12.mp3. In the case of the .pdf the file was 12.pdf.

              Which brings me to another question - how to make it so that the browser gets the correct file name instead of just pulling the id from the url?

              I’ve been unable to do any successful downloads on my 1and1 server. I tried a beta 5 version and RC2-rev6748. On my remote server, there is one other variable that might affect things (although it works fine with my snippet), which is that I use a linux symlink to direct the file manager to a central media location (we have multiple sites that use the same resources, and this saves having to upload them multiple times).
                God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
                • 23491 ☆ A M B ☆
                • 1,056 Posts
                Quote from: mattcdavis1 at May 13, 2010, 09:46 AM

                Any ideas on how I might further troubleshoot this issue? Does anyone currently have Static Resources working correctly (i.e. can use it to download .mp3, .pdf or .flv) for rev6748 or above?

                -matt

                I have a static resource (.JS) working fine on RC-1 (rev6614), but when I first tried linking to an MP3, I received the following:


                Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 4512403 bytes) in /Sites/modx/revolution/core/model/modx/modparser.class.php on line 68

                NOTE: That was using an "application/octet" mime-type for .mp3 ...also, I noticed that even though I checked "Binary" during the setup, that it was not defined as binary when I revisited the Content Types section. I changed the mime-type to audio/mpeg and re-checked binary, and it worked great! (Unfortunately, this does not address your "rev6748 or above" question...)

                Also, my file came through as I would have expected: static1.mp3 (Actual static file name is much longer...)

                HTTP/1.1 200 OK
                Date	Thu, 13 May 2010 23:35:02 GMT
                Server	Apache/2.2.12 (Unix) DAV/2 mod_ssl/2.2.12 OpenSSL/0.9.8k PHP/5.3.0 mod_perl/2.0.4 Perl/v5.10.0
                X-Powered-By	PHP/5.3.0
                Expires	Thu, 19 Nov 1981 08:52:00 GMT
                Cache-Control	public
                Pragma	no-cache
                Content-Disposition	attachment; filename=static1.mp3
                Vary	User-Agent
                Content-Transfer-Encoding	binary
                Content-Length	6513516
                Keep-Alive	timeout=5, max=99
                Connection	Keep-Alive
                Content-Type	audio/mpeg
                


                FWIW, I tried installing rev6807 from SVN, but it failed during the install (Also, I could not Upgrade, as it only allowed the "New Installation" option, but that’s probably for another thread...)

                @OpenGeek, thx for the HttpFox reference, very slick!

                @matt, out of curiousity does your experience change if you give the static resource an Alias? (Are you using Friendly-URLs?)
                  Mike Reid - www.pixelchutes.com
                  MODx Ambassador / Contributor
                  [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
                  ________________________________
                  Where every pixel matters.
                  • 18654
                  • 191 Posts
                  Thanks for the response - I noticed in your http response-header you got:
                  Content-Transfer-Encoding	binary
                  


                  On my install, even with the "binary" box checked, I get:

                  Transfer-Encoding: chunked
                  


                  Quote from: pixelchutes at May 13, 2010, 06:27 PM

                  @matt, out of curiousity does your experience change if you give the static resource an Alias? (Are you using Friendly-URLs?)
                  I’m not currently using friendly urls, but I switched over to friendly URL’s on my localhost, and that definitely takes care of the filename problem, but it didn’t solve the download issue.
                    God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
                    • 20203
                    • 2 Posts
                    Matt, I was wondering if you have made any progress with this issue. I’m running into the same 0 byte file size problem when using Static Resources for PDF files.

                      • 18654
                      • 191 Posts
                      No - I haven’t made any progress. I’m still running rev6748 - so I’m not sure if the issue has been fixed in later SVN versions or not. I hope to download a newer version of SVN soon to see if it’s been fixed. The workaround that I’m currently using is to set all Static Resources to "inline" and then place a snippet in the Static Resource Template which redirects the browser via a header to a location outside of the ModX core. The secondary location contains a regular php download script.

                      @OpenGeek - would you say this a confirmed bug? Should I go ahead and file it in JIRA?

                      -matt
                        God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.