MVC3 Handler.ashx - asp.net-mvc-3

Hi I am calling a Handler.ashx from ActionResult
ViewBag.IMG = "Handler.ashx?img=" + imagetest +
, if the ActionResult is Index it works fine,
http://localhost:11111/ImageHandler.ashx?img=image
however if it is any other name, it does not call the Handler!!
http://localhost:11111/ActionReult(name)/ImageHandler.ashx?img=image
It adds the ActionResult name in the url.
Any Idea, thanks in advance.

Then i not understand false, .ashx instead, can use controller in return FileContentResult or FileResult to File or FileStreamResult to File.
Sample
public FileContentResult Index()
{
var Resim = new WebClient().DownloadData("https://dosyalar.blob.core.windows.net/dosya/kartalisveris.gif");
return new FileContentResult(Resim, "image/png"); //* With {.FileDownloadName = "Höbölö"}
}
Or
public FileResult Index()
{
FileInfo fi = new FileInfo(Server.MapPath("~/Content/imgs/fillo_kargo.png"));
return File(fi.OpenRead, "image/png"); //or .FileDownloadName("Eheheh :)") Return File(fi.OpenRead, "audio/mpeg","Media File")
}
so that by variable public
FileResult Index(string picPath)
FileInfo fi = new FileInfo(Server.MapPath("~/Content/imgs/" + picPath +
"));
Url : File/Index/fillo_kargo.png

Related

Kendo Notification in Code Behind

I have searched and didn't find any solution for my problem. My problem is that I couldn't call notification from my controller and all notification codes are related about razor or js or inside html. I have to write from .cs file and my cs file like that;
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(Kullanici model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
if (string.IsNullOrEmpty(model.KullaniciAdi))
{
//
}
KullaniciDAL kullanicidal = new KullaniciDAL();
Kullanici kullanici = kullanicidal.GetRowByKullaniciAdi(model.KullaniciAdi);
if (string.IsNullOrEmpty(kullanici.KullaniciAdi))
{
//
}
string s = Code.Dev.Crypto.Decrypt(kullanici.Sifre);
if (s == model.Sifre)
{
HttpCookie cookie = new HttpCookie("CustId", kullanici.CustId);
cookie.Expires = DateTime.Now.AddDays(2);
HttpContext.Response.Cookies.Add(cookie);
cookie = new HttpCookie("KullaniciAdi", kullanici.KullaniciAdi);
cookie.Expires = DateTime.Now.AddDays(2);
HttpContext.Response.Cookies.Add(cookie);
cookie = new HttpCookie("KullaniciRol", "Uzman");
cookie.Expires = DateTime.Now.AddDays(2);
HttpContext.Response.Cookies.Add(cookie);
return RedirectToLocal(returnUrl);
}
else
{
//notification will be showed
//Kendo.Mvc.UI.NotificationTemplate notification = new Kendo.Mvc.UI.NotificationTemplate();
//notification
return View("Login");
}
}

how to pass class parameter through Rotativa.ActionAsPdf

I want to pass class parameter in ActionAsPdf
public ActionResult Pdf(long Id)
{
var printclass = this._printService.GetPrintResults(Id);
return new ActionAsPdf("Content", new {Id = Id})
{
FileName = "abc.pdf"
}
}
public ActionResult Content(long Id)
{
//viewModel
return View("Index", viewModel);
}
It's working fine if Id alone is passed. But I want the printclass (var printclass of type class) to be passed in as the parameter as well to the Content.
I am having problem when I try to pass the class like below.
return new ActionAsPdf("Content", new {Id = Id, printclass= printclass})
{
FileName = "abc.pdf"
}
public ActionResult Content(long Id, printDTO abc)
{
var temp = abc;
//viewModel
return View("Index", viewModel);
}
The value of temp is null in the above case.
Use ViewAsPdf() instead. ActionAsPdf() accepts a RouteValueDictionary parameter.

download pdf files stored in database in mvc3

I store few pdf files in my db as binary format using the below code in my controller,
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
Image newImage = new Image();
newImage.MimeType = file.ContentType;
var binaryReader = new BinaryReader(file.InputStream);
newImage.Data = binaryReader.ReadBytes(file.ContentLength);
binaryReader.Close();
objImage.InsertImage(newImage.Data);
return View();
}
now i want to download them back based on the id passed to the contrller that pdf files should be downloaded??
this is my code for pdf download, wat do i need to add more
public ActionResult Download(int id)
{
DataSet da = new DataSet();
da = objImage.getUserImage(id);
DataTable dt = new DataTable();
dt = da.Tables[0];
Byte[] imagedata=(Byte[])dt.Rows[0]["UsImage"];
}
this is my code for pdf download, wat do i need to add more
Return an ActionResult:
public ActionResult Download(int id)
{
...
byte[] imagedata = (byte[])dt.Rows[0]["UsImage"];
return File(imagedata, "image/png");
}
and if you want the browser to popup a Save As dialog instead of displaying the image inline specify a filename:
public ActionResult Download(int id)
{
...
byte[] imagedata = (byte[])dt.Rows[0]["UsImage"];
return File(imagedata, "image/png", "foo.png");
}
Obviously the MIME type and the filename could come from your database as well. In this example I have hardcoded them but you could adapt this code.
return File(result.Content, result.Extension.Replace(".", ""));
public ActionResult Download(int id)
{
DataSet da = new DataSet();
da = objImage.getUserImage(id);
DataTable dt = new DataTable();
dt = da.Tables[0];
Byte[] imagedata=(Byte[])dt.Rows[0]["UsImage"];
return File(imagedata, "image/png");
}
public ActionResult GetPdf(int id)
{
ProjectProfile projectprofile = db.ProjectProfiles.Find(id);
var image = projectprofile.pdf;
return File(image, "application/pdf");
}

Calling Save dialog box from javascript

I have a javascript function from which I need to call a controller action to return the filestream to the UI .
I am not getting the open,save and save as dialog box.
In the cshtml file I have following function:DownloadFile
var selectUrl = '#Url.Action("Download", "Controller")' + "/" + filedetails;
$.post(selectUrl);
and in the controller I have the following code:
public ActionResult Download(string id)
return File(downloadStream, "application/octet-stream",fileName);
Please let me know is this the correct way of calling.
try this way :ActionResult
public ActionResult Download(string id)
{
var cd = new System.Net.Mime.ContentDisposition
{
FileName = "imagefilename",
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
string contentType = "application/octet-stream";
// you are downloadStream
return File(downloadStream, contentType);
}
link here

How to Generate pdf of details view in mvc 3

I just want to generate a pdf document of the details presents in view on button click.
In order to generate a PDF file you will need some third party library as this functionality is not built-in the .NET framework. iTextSharp is a popular one.
So for example you could write a custom action result:
public class PdfResult : ActionResult
{
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
response.ContentType = "application/pdf";
var cd = new ContentDisposition
{
Inline = true,
FileName = "test.pdf",
};
response.AddHeader("Content-Disposition", cd.ToString());
using (var doc = new Document())
using (var writer = PdfWriter.GetInstance(doc, response.OutputStream))
{
doc.Open();
doc.Add(new Phrase("Hello World"));
}
}
}
and then have your controller action return this result:
public class HomeController : Controller
{
public ActionResult Index()
{
return new PdfResult();
}
}

Resources