简体   繁体   中英

Save and access file on a webserver and localhost

I would like to save a file on a webserver from my fully-working fileupload control. I have received the HttpPostedFile file = HttpContext.Current.Request.Files[0]; and I would like to save it on my server.

My servers is called "appharbor" ( https://appharbor.com/ ) and it's specification say, that I have file write access for Path.getTempPath(); folder.

My code for saving the file is:

HttpPostedFile file = HttpContext.Current.Request.Files[0];
string dirrectoryPath =Path.GetTempPath();
string fileName = Path.GetFileName(file.FileName);
string mapPath = HttpContext.Current.Server.MapPath(dirrectoryPath);
Directory.CreateDirectory(mapPath);
string fileFullPath = Path.Combine(mapPath, fileName);
file.SaveAs(fileFullPath);
myModel.uri = fileFullPath;

How I can remake this code in a working way?

View:

String pdf = "http://docs.google.com/gview?url=" + @Model.uri + "&embedded=true";
<iframe src="@pdf" style="width:100%; height: 1000px;" frameborder="0"></iframe>

As You can see, we are ONLY trying to DISPLAY the file on webpage. Not allowing the user to download it (no special restrictions for it, however).

EDIT

Detailed information what happens now and with slight change:

When I have

string dirrectoryPath = Path.GetTempPath();

then that occurs on server:

POST http://warsztatynet.apphb.com/api/upload 500 (Internal Server Error) 

and that occurs locally

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
Additional information: 'C:/Users/Tunczyk/AppData/Local/Temp/' expected virtual path.

And I suspect that they're exactly the same thing, becaue both of these happen in same function.

Path.GetTempPath(); its App_Data folder on webserver and on localhost

When I have

string dirrectoryPath ="~/App_Data";

then that occurs on server:

GET http://c/Users/MyPath/sample.rtf 404 (Not Found)

and that occurs locally:

GET http://c/Users/MyPath/sample.rtf 404 (Not Found) 

EDIT 2 It's answer for Caótico fanegas post:

Not allowed to load local resource: file:///D:/Users/apphb5dac91d9317868/AppData/Local/Temp/glyphicons-halflings.png 

This is what happens when I do it like that:

string dirrectoryPath = Path.GetTempPath();
string fileName = Path.GetFileName(file.FileName);
Directory.CreateDirectory(dirrectoryPath);
string fileFullPath = Path.Combine(dirrectoryPath, fileName);
file.SaveAs(fileFullPath);

Guys I'm looking for easiest way to do this. Its not commercial application. It is just for university.


EDIT 3

To be more understandable, I will give You a link to my application: MyWebsite To test it You have to:

  1. press Course Creation Tab
  2. Press button +
  3. choose any lesson Schema by pressing it
  4. Press any of coloured divs
  5. Choose a file type from list
  6. Upload file

There Should be displayed Your file, but it isn't (in main are of the view - clicked div).

I recomend to use Google Chrome because at other browser You may have some visual problems.

You'll have to store the file anywhere inside the provided path, and then use an action to retrieve that file, using the FileResult action result

public FileResult file(string fileName)
{
    return File(Path.Combine(Path.GetTempPath(), fileName), System.Net.Mime.MediaTypeNames.Application.Octet);
}

This is not the full solution, but should put you in the right track.

Looks like HttpPostedFile.SaveAs() expects a virtual path to save files. The "~/App_data" approach should work if you don't translate it to local path through Server.MapPath() .

If by any chance you are forced to save to TempPath (trouble with permissions, for instance), I guess you could use the Stream.CopyTo() method to save the file on a random physical path.

using (var newFile = System.IO.File.Create("local_path"))
{
    fil.InputStream.CopyTo(newFile);
}

This code comes straight from my head, and so, is not 100% reliable. Might need some work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM