Writing Excel using EPPLUS not workable after deployed - asp.net-mvc-3

I'm using EPPlus to generate excel (.xlsx) in my mvc3 web application. It works fine with my localhost. However, after i deployed to the server, the excel file return as an Excel Binary Workbook. Can anyone help me?
Here is how i generate my excel (.xlsx):
ExcelPackage pck = new ExcelPackage();
ExcelWorksheet ew = pck.Workbook.Worksheets.Add("template" + templateVM.Year);
List<YieldsTemplate> list = mgr.GetTemplates(templateVM.Year);
ew.Cells["A1"].LoadFromCollection(list, true);
ew.Cells["A:AZ"].AutoFitColumns();
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment: filename=" + "abc" + ".xlsx");
Response.BinaryWrite(pck.GetAsByteArray());
Response.End();
thank you!!

Related

How to upload image to server using web api?

I am at learning phase of web api and I want to upload image to server using web api in mvc dot net. I have tried much but not getting solution.
In controller I have done this.
string img = username + "_" + labTestId + fb;
var element2 = image;
MemoryStream ms = new MemoryStream(Convert.FromBase64String(element2));
byte[] imagesbytes = ms.ToArray();
string folder_name = "lab-orders";
folder_name = folder_name.ToLower();
string SaveLocation = ServerConfig.BlobPath + folder_name + "/" + fb+imagesbytes;
BlobUploader.UploadTo(folder_name,img, ms);
// var filePath = HttpContext.Current.Server.MapPath("~/Userimage/" + postedFile.FileName + extension);
var filePath = HttpContext.Current.Server.MapPath(SaveLocation + postedFile.FileName + extension);
postedFile.SaveAs(filePath);
How can I upload image to server using web api?
It's not that simple.
In this tutorial You have step by step everything You have to do, to upload image.
In brief:
Create Blob Storage Account, and get access keys.
Save keys in Your web config.
Install WindowsAzureStorage from nuget packege manager
Create blob storage client, get or create blob container
create CloudBlockBlob and upload it.
Edit
Change link to web api tutorial insted of mvc tutorial

How to open generated PDF in MVC

I do AJAX call to generate PDF from crystal report.
Problem is , how to open PDF directly.
Below is my stuff. It looks, pdf is created but, it just return to view and not able to open PDF.
Please guide me.
Code:
public ActionResult CreatePDF(string paramValue)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("FieldName");
dt.Columns.Add(dcinitial);
DataRow dr = dt.NewRow();
dr[0]=paramValue;
dt.Rows.Add(dr);
ReportDocument oRpt = new ReportDocument();
string path = Server.MapPath("~/PDFDocs/crystalreport.rpt");
oRpt.Load(path);
oRpt.SetDataSource(dt);
MemoryStream oStream = new MemoryStream();
oStream = (MemoryStream)oRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
string fileName = "Report";
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(oStream.ToArray());
Response.End();
return View();
}
Thanks
Don't write to the Response in the controller. Aside from coupling issues and testability, it's confusing the functionality of that controller action. This code is trying to both write the file to the response and return the view.
Instead, just return the file, which you can do directly from the stream:
return File(oStream, "application/pdf", fileName);
However...
I do AJAX call
That's going to complicate things a bit. Ideally this wouldn't be an AJAX call. This is because AJAX doesn't handle "files" in a way you might think. There are options, but it would probably be easier to make this a normal page-level request so the browser can handle the file natively.
Since it's returning a file and not a view, the browser won't unload the current page. (Unless it's configured to display the file instead of save it, though there isn't much you can do about that. The server-side code is properly suggesting that the browser save the file by providing a Content-Disposition header.)

How to convert rad controls to images in silverlight

I'm using rad controls(charts and gridview) for developing an application,which i need to export the controls(each) into image.I have tried each control converting them into bytes format and send to webservice and converting them to images but sometime sending the byte data to service throws an error.Is any other way to convert each control into image.I have tried another way like.
Stream fileStream = File.OpenRead(#"\\HARAVEER-PC\TempImages\FlashPivot.png");
//PART 2 - use the stream to write the file output.
productChart.ExportToImage(fileStream, new Telerik.Windows.Media.Imaging.PngBitmapEncoder());
fileStream.Close();
It throwing me an error like cannot access to the folder TempImages.I have given sharing permissions to everyone but it doesn't access the folder.
Any solution is much appreciated.
private BitmapImage CreateChartImages()
{
Guid photoID = System.Guid.NewGuid();
string photolocation = #"D:\Temp\" + photoID.ToString() + ".jpg";
BitmapImage bi = new BitmapImage(new Uri(photolocation, UriKind.Absolute));
using (MemoryStream ms = new MemoryStream())
{
radChart.ExportToImage(ms, new PngBitmapEncoder());
bi.SetSource(ms);
}
return bi;
}

File Download from Azure Blob

Below code is for donwnloading file from azure blob. I have problem with .docx,.xlsx files and that too after deployment only, mean in local machine it is working fine.
The problem is after downloading .xlsx or .docx, when i open that file showing file corrupted popup.
public void DownloadBlob(string blobName)
{
//You have to get values for below items from azure
string accountName = "MyAccName";
string accountPrimaryKey = "MyKey";
string blobContainer = "ContainerName";
CloudStorageAccount account = new CloudStorageAccount(new StorageCredentialsAccountAndKey(accountName, accountPrimaryKey), false);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(blobContainer);
CloudBlob blob = container.GetBlobReference(blobName);
MemoryStream memStream = new MemoryStream();
blob.DownloadToStream(memStream);
Response.ContentType = blob.Properties.ContentType;
Response.AddHeader("Content-Disposition", "Attachment; filename=" + blobName.ToString());
Response.AddHeader("Content-Length", (blob.Properties.Length - 1).ToString());
Response.BinaryWrite(memStream.ToArray());
Response.End();
}
I am sure you have issue with your code as As Steve suggested you are setting length incorrectly.
I worked on similar issue sometime last year and documented the solution in my blog as below:
http://blogs.msdn.com/b/avkashchauhan/archive/2011/04/05/downloading-word-and-excel-files-from-windows-azure-storage-in-a-asp-net-web-role.aspx

Error occured while export data from excel

I am getting an error while exporting data to an excel sheet.
Code
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=CompletionDatesReport.xls")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.xls"
Dim stringWrite As StringWriter = New StringWriter()
Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWrite)
gridData.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
Error
Internet explorer cannot download abc.aspx from www.xyz.com.
Internet explorer was not able to open this internet site.The requested site is either unavailable or cannot be found.Please try again later.
Remove the line no 4
Response.Cache.SetCacheability(HttpCacheability.NoCache)
It will be work.

Resources