2011-10-01 16 views
6

मैं डेटा निर्यात करने के लिए, वास्तव में मैं लागू कर दिया है, लेकिन जब निर्यात एक्सेल देखने के लिए Excel के रूप में देखने के लिए फ़ाइल (MVC)

return new FileContentResult(fileContents, "application/vnd.ms-excel"); 

उपयोग करने के लिए बनाम मेरी संदेह नहीं है

return File(fileContents, "application/vnd.ms-excel"); 

और इस विधि में से प्रत्येक में डाउनलोड करने योग्य फ़ाइल नाम कैसे सेट कर सकते हैं?

उदाहरण 1:

public ActionResult ExcelExport() 
{ 
    byte[] fileContents = Encoding.UTF8.GetBytes(data); 
    return new FileContentResult(fileContents, "application/vnd.ms-excel"); 
} 

उदाहरण: 2

public ActionResult ExcelExport() 
{ 
    byte[] fileContents = Encoding.UTF8.GetBytes(data); 
    return File(fileContents, "application/vnd.ms-excel"); 
} 

उत्तर

9

आप FileContentResult के बीच मतभेद & FileResult यहाँ के बारे में पढ़ सकते हैं: What's the difference between the four File Results in ASP.NET MVC

आप इस

की तरह फ़ाइल नाम निर्दिष्ट कर सकते हैं
return new FileContentResult(fileContents, "application/vnd.ms-excel") { FileDownloadName = "name.xls" }; 

// or 

// note that this call will return a FileContentResult object 
return new File(fileContents, "application/vnd.ms-excel", "name.xls"); 
संबंधित मुद्दे