Share via


Server.MapPath & Mapped Network Folder/Drive

Question

Thursday, December 5, 2013 2:07 PM

As part of my FileUpload helper on a webpage, I currently upload videos and store them in a local folder. However, this particular server that the website is housed in has limited space and further down the road, the vidoe files will max out the max capacity of the server.

So I want to store the video files on a different server which can be connected via mapped drive. However, I've been trying to figure out how to establish connection to that mapped drive so I can dropped the video files there.

Clearly this does not work:

fileSavePath = Server.MapPath("\\server\mapped folder\" + videoName);

Is there an alternative solution?? Many thanks in advance!!

All replies (7)

Thursday, December 5, 2013 2:11 PM ✅Answered

fileSavePath = @"\\server\mapped folder\" + videoName;

should work. Server.MapPath is only for virtual paths and can't take a filesystem path.

The limitation to the above is that the account the website is running under must have permissions to write to that directory. Normally it definitely wouldnt even if the share is available from the web server. You'll need to figure out which account the application pool is running under and grant that account rights to write to the folder in the share.


Thursday, December 5, 2013 2:14 PM ✅Answered

You'll need to look into "Impersonation".  The concept is that you can programatically have ASP.NET access the system under a specified user's credentials, specifically one that has r/w access to the network drive you are trying to save to.  There are a lot of parts to this, but I posted a solution to a similar question a while back that includes a class I wrote for handling Network files.  It was built to function similarly to the File namespace in ASP.NET, albeit with less functionality. It doesn't directly support the SaveAs functionality used in file Upload code, but maybe this can get you started.

http://forums.asp.net/t/1730462.aspx?Access+to+a+file+share+location

As for Mark's solution, he is right.  You could also grant r/w access network directory access to the account that is being used by the application pool.  This sometimes won't work if the account isn't a domain account, but you could also configure IIS to run the application pool using a domain account, which might also work.


Thursday, December 5, 2013 2:40 PM

Thank you for your prompt response!

It works! That is because the two servers are within the same account in which the application pool is running and so each account already have rights to read/write for the IIS users.

However, it works when uploading -- but didn't work when attempting to retrieve or simply view the video file online. Is there a different approach to this??


Thursday, December 5, 2013 2:44 PM

How are you trying to "retrieve or view" it?  You would need to write code that presents it to the user programatically.  You could write an HTTP handler to do this.


Thursday, December 5, 2013 4:44 PM

Ahh okay, what I do is I use JW Player to view the video file.

So when I use the full path of \server\mapped folder\ + videoName, the video file is not found.

Essentially, I have something like this:

@{
var video = {video name retrieved from database}
var videoPath = @"\\server\mapped folder\";
var fullVideoPath = videoPath + video;
}

<body>
<div>
    <script type="text/javascript">
        jwplayer("myElement".setup({
            file: "@fullVideoPath",
            title: "@video",
            width: 920,
            height: 500
    </script>
</div>
</body>

When I had the video stored in the same server but different folder, this works just fine. However, I'm having the video stored in a different server connected via mapped drive and this time around, it doesn't work. So while I'm able to successfully store video, I'm unable to "retrieve" them through the player where it would have otherwise worked if the folder was local and not a mapped drive.

Hope this is more clear!


Thursday, December 5, 2013 5:22 PM

So you want your javascript to be able to identify a network path?  

An http handler could definitely do this, though I've never written one to deliver video content.  Here is one I made for downloading files:

File.ashx

<%@ WebHandler Language="C#" Class="DownloadHandler" %>

using System;
using System.IO;
using System.Web;

public class DownloadHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        String fileName = (context.Request.QueryString["path"] != null) ? Convert.ToString(context.Request.QueryString["path"]) : String.Empty;
        if(File.Exists(context.Server.MapPath(fileName)))
        {
            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName.Substring(fileName.LastIndexOf("/")+1));
            context.Response.ContentType = "text/plain";
            context.Response.WriteFile(context.Server.MapPath(fileName));
        }
        else
            context.Response.Write("File Not Found");
    }
    
    public bool IsReusable
    {
        get{return false;}
    }
}

And to use it, you could do something like: http://www.mysite.com/File.ashx?path=someFile.pdf, which opens up the download dialog.

I wrote this once upon a time because I wanted users to be able to download pdf files, instead of opening them in the browser, which happens when the mime type is identified in IIS.  You could use a similar concept, instead setting the ContentType to a video format of some sort instead of text/plain (you might have to check the file extension or something).  This gives you a level of separation from the client and the actual storage of files where if you decided to change where files are being stored, the client code won't need to be updated, instead you can manage that in the ashx file.


Monday, December 16, 2013 3:43 PM

Thank you for the suggestion, but I'd rather not use Javascript -- I'd like to keep the programming server-side to be able to pull the video from a separate network folder and view the video.

So I'm very confused as to why I'm able to successfully upload a video into that network folder and yet I cannot pull the same video to view it via online player??