Downloading files in asp.net mvc - asp.net-mvc-3

I have a link that allows the user to download a file. Basically I call the controller action that uses base.File function. I have few different types of files - pdf, doc, text....Is there a contentType I can specify that would work for all these? Or do I need to specify the correct (matching) contentType for each and every file type?

Use "application/octet-stream" for general purposes.

Related

Firefox-Addon: Add search engine with varying URL and suggestions

my Firefox addon shall add a search engine, that
provides suggestions
gets its search template URL specified on runtime (i.e.: template URL depends on the preferences of the user)
And I don't see a way to do both at the same time.
I see two options to add a search engine:
addEngineWithDetails
addEngine
addEngineWithDetails() allows me to add a search engine with the template URL. But it does (apparently?) not allow to provide a suggestions URL.
addEngine() allows me to add a search engine that is specified in an XML file. But if have that file saved locally in my addon directory (e.g. chrome://example-engine/content/search.xml), how can I change the template URL on runtime? And using an online XML is an unsafe options since the internet connection could be broken or bad during the addon install.
First fo all, you're right, addEngineWithDetails does not support suggestions.
The way to go would be to use addEngine (and removeEngine).
As for the "dynamic" part of your question: While I didn't test it, the implementation seems to happily accept data: URIs. So you could:
Construct a data URI using whatever methods you like (even constructing a full XML DOM and serializing it).
Call addEngine with the data URI.
When the user changes a pref, remove the old engine, and construct a new one.

Simple way to response some json in Webmatrix 2.0?

I'm working on a simple web project for mobile in Microsoft Webmatrix 2.0. For testing purposes, I just want to put a .json file in my project and use that in my javascript code.
Is it possible to set a mime type for different file extensions in Webmatrix? I want my file to response as application/json.
I'll answer my own question. I used to have my code in a blank project, but in order to serve json data with correct mime type, I had to start a new Razor enabled project. I then added a .cshtml file with the following peiece of code:
#{
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.Write("{\"myCollectionOfObjects\":[{...,...}]}");
}

Recommended way to output plain text from a RESTful controller to PDF

I'm looking for a way to automatically have my RESTful controller, which returns a String, output a pdf file to the calling browser, when the URI has a .pdf at the end:
http://localhost:9090/services-rs/notices/58357.pdf
Without the .pdf at the end, it currently merely returns the String, i.e. plain text, in the browser.
I tried adding:
<entry key="pdf" value="application/pdf"/>
to my mediatypes list in my ContentNegotiatingViewResolver, but evidently that didn't do the trick.
How do I go about this? Is there a 3rd party library I need to use, or does Spring MVC have this ability built-in?
The ContentNegotiatingViewResolver does not use the extension at the end of your URI. It uses the Accept header in the request. If you would like to use the ContentNegotiatingViewResolver, ensure that your Accept header on the client side is using application/pdf.
If this is not possible for you, you will need to have a different controller.

Programatically retrieve an attachment stored on a note on a CRM 4.0 entity

How would you suggest working with files that is stored on the note of a entity in Crm. Could you write a generic method that will enable you to access any type of file? Or would it be better to have a method for dealing with each type of file?
For example, we are going to be saving a mix of swf files and xml files on the entity, so would it make sense to have a method each for example:
GetXmlFilesOnAccount(accountid)
GetSwfFilesOnAccount(accountid)
When you upload an attachment to CRM the mimetype is also saved as part of the record information.
The following link contains a nice example of how to download the attachemt using a single method. http://crmscape.blogspot.com/2009/10/ms-crm-40-sending-attachments-to.html
The post is missing the actual query needed to retrieve the annotations but you can tell what columns are required from the method signature.
My suggestion using your methods:
* GetXmlFilesOnAccount(accountid)
* GetSwfFilesOnAccount(accountid)
Retrieve account activitypointers by regardingobjectid(in your case accountid guid)
Loop through returned activitypointers
Get attachments for each activitypointer (activitypointer.activityid = activitymimeattachment.activityid)
Store attachments (disk, etc)
You don't even need two methods. You can retrieve all attachment file types for a given note (annotation) with a single method.
Hope this helps.
I recently started an Open Source Project on CodePlex to accomplish exactly that. Feel free to check out the Project's Web Page at:
http://crmattachdownload.codeplex.com/
You can also view the source code under the "Source Code" tab of that same page.
Pete

Downloading CSV via AJAX

Can you use AJAX to download a generated csv file from a web application? If so does anyone have any kind of reference that I could be pointed towards?
EDIT: Sorry I should have mentioned I am using Prototype's Ajax.Request and I looked in firebug's response tool and the generated CSV is the response, I just need to get it to pop up with the save file option after has been generated by the Ajax.Request
This is a known limitation of Ajax requests, you will need to use JS like:
window.location='download-csv.rb';
Instead of using an Ajax request. Another way is to change the location of a hidden Iframe, but this has it's own pro's/con's.
You will never get an Ajax request to display the 'file save' dialog, no matter what HTTP headers you send.
In light of your latest edit, to make your CSV file trigger a file download (instead of rendering in the browser), there's no need for Ajax.
Instead, the solution is to have your back-end system add this HTTP header when the CSV file is requested:
Content-disposition: attachment; filename=<your_filename.csv>;
Your implementation here depends on the back-end system you're using. If you're using Rails (as your username suggests), here's a start:
filename = 'your_filename.csv'
headers['Content-Type'] = 'text/plain'
headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
render :layout => false
Downloading it isn't the problem; you can download any data you like via XmlHttpRequest. The hard part is parsing it. There are several ways to parse it, from regexs to string indexing.
You can use "AJAX" to download anything .. Some people would say you shouldn't call it AJAX in that case since that term is rigorously devoted to downloading XML. But really it's just a mechanism to get data into the client w/o reloading a page. If you were loading HTML it'd be called AHAH, for CSV i guess you'd call it AHAC or AJAC? ..

Resources