0

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);
Niranjan Godbole
  • 2,135
  • 7
  • 43
  • 90
  • Have you noticed the missing letter > function ShowFiepopup(fileName) – norcal johnny Sep 26 '16 at 07:44
  • i corrected that. Browser error message says Not allowed to load local resource: file:///F:/UploadedFile/1007_20160926131603798_24e5723e-8044-4906-9cdb-8dbd1271232d.pdf. Do i need to return filename or bytes? ../UploadedFile/ ts not the right path. Should i give F:/UploadedFile? – Niranjan Godbole Sep 26 '16 at 07:48
  • I suspect the clue is in the error message: *Not allowed to load local resource* - looks like you're not allowed to access that path. – freedomn-m Sep 26 '16 at 08:50
  • Yes i am getting that error message. How can i access files stored outside IIS server? – Niranjan Godbole Sep 26 '16 at 08:51
  • I see that you need relative path instead of absolute path to your file, since browsers may running in sandbox mode that restrict ability to reach the local file system. Probably `IUrlResolutionService` become the best choice to map relative path of your file. – Tetsuya Yamamoto Sep 26 '16 at 08:52
  • Yes. May i have some usefull link so that i can fix the above? Also i need to return filename or bytes to jquery? Thank you – Niranjan Godbole Sep 26 '16 at 08:53
  • AFAIK, there's other way to map relative path besides using `IUrlResolutionService`: use `Uri.MakeRelativeUri(target)`. This reference may help you: http://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path – Tetsuya Yamamoto Sep 26 '16 at 09:00
  • Thank you. I gone through above link. So for example if my files are stored in F:/uploadedfile/abc.pdf then what path it will return(method name MakeRelativePath) – Niranjan Godbole Sep 26 '16 at 09:09
  • In the above example i should return filename or relative path to jquery? – Niranjan Godbole Sep 26 '16 at 09:10
  • http://stackoverflow.com/questions/11341968/create-relative-path-using-system-uri i have refered. Can someone tell me what i should return to jquery? – Niranjan Godbole Sep 26 '16 at 09:28
  • "How can I access files stored outside IIS server". You can't. If the files are on your server and you want them accessible through the browser/javascript then you need to make it possible to serve them via IIS. You don't necessarily need to make folder itself into a servable location if that's a security risk, you can instead write a server-side script which will take suitable input (e.g. filename, some ID, whatever) and locate and return the correct file for download. – ADyson Sep 26 '16 at 09:54
  • For example in IIS we will keep all controller,views,javascripts and all. Outside that application root path for example Eor F drive on the same pc. Can we keep files and access? – Niranjan Godbole Sep 26 '16 at 10:00
  • @NiranjanGodbole Yes you can keep them outside the application and access them - but like I explained you'll need some server-side script (e.g. in C#) maybe as a little webservice which can accept some sort of identifier as input and then find the file on disk and return it to the user for download. So it's like indirect access. This can also help security because you can, if you wish, have logic in your service code to determine who can access which files. – ADyson Sep 26 '16 at 11:14
  • Thanks Adyson... Yes actually i have one folder called uploadedfile and my basic purpose is to stop direct access of files from that folder. I kept files outside IIS and i am able to save files. I am getting problem in downloading. – Niranjan Godbole Sep 26 '16 at 11:44
  • yes you can save files because your C# code can write to the disk. But you can't directly retrieve them with javascript. So you either put the files directly accessible in IIS via a URL, or you write some more C# to retrieve them based on a unique identifier, and make your javascript call that C# script and pass in the identifier. You already have a "Download" Actionresult, so all you need to do it make it point at the right location. Currently your jQuery is not calling it though. – ADyson Sep 26 '16 at 13:19
  • Thank you for response. How can i make files directly accessible in IIS via a URL? also how can i implement jquery after returning URL? Suggest me some examples or links. Thank you – Niranjan Godbole Sep 27 '16 at 04:34

0 Answers0