In one of my mvc4 application I am uploading files and keeping it in F:\uploadedfile\filename.pdf path. also i hosted the application local IIS. Below is my actionmethod to download file.
public ActionResult Download(int? ClientId)
{
service.Service objService = new service.Service();
DashboardBAL objdb = new DashboardBAL();
string filepath = objdb.GetFilepath(ClientId.GetValueOrDefault(0));
string folderName = @"F:\UploadedFile";
//string serverMappath = ("~/UploadedFile");
string serverMappath = (folderName);
byte[] result = objService.DownloadFileFromDMS(filepath);
string contentType = MimeMapping.GetMimeMapping(filepath);
string FileName = Path.GetFileName(filepath);
var cd = new System.Net.Mime.ContentDisposition
{
FileName = Path.GetFileName(filepath),
Inline = true,
};
if (!System.IO.Directory.Exists(serverMappath))
{
System.IO.Directory.CreateDirectory(serverMappath);
}
string strdocPath = serverMappath + @"\" + FileName;
if (result != null)
{
FileStream objfilestream = new FileStream(strdocPath, FileMode.Create, FileAccess.Write);
objfilestream.Write(result, 0, result.Length);
objfilestream.Close();
}
return Json(FileName, JsonRequestBehavior.AllowGet);
}
This is jquery code.
function ShowFiepopup(fileName) {
$("#dialog").dialog({
modal: true,
title: "Preview of " + fileName,
width: 850,
height: 600,
buttons: {
Close: function () {
$(this).dialog('close');
}
},
open: function () {
var object = "<object data=\"{FileName}\" type=\"application/pdf\" Zoom=\"100%\" width=\"800px\" height=\"600px\">";
object += "If you are unable to view file, you can download from <a href=\"{FileName}\">here</a>";
object += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
object += "</object>";
object = object.replace(/{FileName}/g, "../UploadedFile/" + fileName);
$("#dialog").html(object);
}
});
};
I am unable to download files in above code. I am returning only file name. Do i need to return bytes and pass it to Jquery code? In the below line of code How can i bind bytes to object?
object = object.replace(/{FileName}/g, "../UploadedFile/" + fileName);
Can someone tell me? thanks in advance.
i changed as below.
System.Uri uri1 = new Uri(strdocPath);
System.Uri uri2 = new Uri(serverMappath);
Uri relativeUri = uri2.MakeRelativeUri(uri1);
string FileName1 = Uri.UnescapeDataString(relativeUri.ToString());
return Json(FileName1, JsonRequestBehavior.AllowGet);
Now what i should replace in below line of code?
object = object.replace(/{FileName}/g, "F:/UploadedFile/" + FileName);