0

I want to convert a URL like http://localhost/app/Uploads/file.png to my application virtual path, i.e., ~/Uploads/file.txt.

My question is how can it be done in asp net mvc?

EDIT: some explanation on my scenario for further clarification.

I have a "Create" form. In this form, the user sets up its profile and can upload a photo to the server. This photo can be previewed by async uploading it to the server. This is accomplished by the action Post() of the FileUploadController, which is an ApiController. This action simply saves the uploaded file to the virtual path "~/Uploads/" and then returns the full URL to the saved file, something like http://localhost/app/Uploads/file.png. Then, the javascript on the page catches this url and updates a <img> tag to display the picture and sets the value of an hidden input field with this URL. This works fine.

Now, I have to implement the Submit action of this "Create" form. Among other things, the action viewmodel receives the URL of the photo, i.e., http://localhost/app/Uploads/file.png, which should be used to copy the file 'file.png' from the temporary location "~/Uploads/file.png" to another location. However, to Copy this file, I need the local path to it and all I have is the URL.

Richard Lee
  • 2,136
  • 2
  • 25
  • 33
TapiocaCom
  • 353
  • 5
  • 16
  • 2
    Where do you get that original URL? If you just want to create a link to that file then you'd use `Url.Content("~/Uploads/file.txt")`, the fully-qualified version would never be needed. – David Jun 25 '14 at 19:48
  • The original URL is from Url.Content("~/Uploads/file.txt"). However, it is done in another Action. – TapiocaCom Jun 25 '14 at 19:50
  • 1
    Sounds like a duplicate of http://stackoverflow.com/questions/6081433/getting-relative-virtual-path-from-physical-path – Wiktor Zychla Jun 25 '14 at 19:53
  • It's not really clear what the problem is here. Perhaps you can elaborate on the end-to-end functionality you're implementing and where specifically it's failing? – David Jun 25 '14 at 19:53
  • 1
    It sounds like you want to reverse a FQDN to the *~short*. However you haven't explained why you want to do this, so it REALLY sounds like an [XY Problem](http://meta.stackexchange.com/a/66378/171858). In other words you think that being able to do this will solve some root problem, but you haven't described the root problem this would solve. Most likely, this is not the way to solve the root problem – Erik Philips Jun 25 '14 at 19:53
  • I have edited for clarification. – TapiocaCom Jun 25 '14 at 20:09
  • The first issue is really why the *save* action is returning a full url instead of a relative path. A relative path would fix all your issues. – Erik Philips Jun 25 '14 at 20:51
  • Erik, really... I know this is a solution, but you are assuming I can do anything with the code, which is not true in real development teams. Furthermore, my original question should have a simple answer and regardless of the reason I asked this question, I would like to know whether a simple answer does exist... – TapiocaCom Jun 25 '14 at 21:03
  • Yes I made an assumption, because there was no requirements in your question that you could not change that behavior. You're assuming that everyone here should only assume that the question being asked is the right question (which is rarely the case, regardless of *real development teams* or not). I hope you find your answer, but I typically frown upon using a work-around instead of fixing the root issue. In an agile/scrum environment your problem would be roadblocked and a new bug added to the other team who would fix the problem (agile) as soon as possible. – Erik Philips Jun 25 '14 at 22:04
  • This is not true. I cannot change the return value of the Upload Action because it is being used everywhere around and the system is huge! changing it is too riscky and takes time. I appreciate your efforts to go deeply into the problem and find a better solution, but if I needed to write down a whole precise text describing the whole context it would get too painful to ask anything here. Again, the question is simple and it remains the same. – TapiocaCom Jun 26 '14 at 00:05

1 Answers1

3

You can do something like this. Write this in your razor view to get the root-relative path

<script type="text/javascript">
   var baseUrl = '@Url.Content("~")';
</script>

Then in your submit button's client action you can access and pass localpath to controller's action method for file copy.

$("#submit").click(function () {
    var localpath = baseUrl + "Uploads/file.png";
    ...
    ...
});

Here is the link to my another post to almost a similar problem.

Community
  • 1
  • 1
Dennis R
  • 3,195
  • 1
  • 19
  • 24