Write file in Azure App Service using MVC - asp.net-mvc

Is it possible to create file in App_Data dir of MVC app while its running on Azure- App Service (Not Virtual Machine)?
Actually I have to download images from URL and save to the App_Data folder for further processing. I am able to do this in development laptop. I get the path of my App_Data folder using the below line and save files there:-
String localFileName = Server.MapPath("..\\App_Data") + "\\" + saveFileName;
I am not sure it is possible in the Azure's App Service.
What can be alternative way of doing it?
Please help.

Related

How to add a file to a virtual directory in Azure App Service

I have asp.net mvc application hosted on Azure as an App Service....Example.com
I need to have a location where my Google Shopping Feed can be hosted as a text file, so I created a Virtual Directory in my Azure portal for that Example.com App Service.
The virtual directory is Example.com/myvirtualdirectory
I created this by going to (Configurations > Path Mappings > + New Virtual application or directory) I named the virtual path as /myvirtualdirectory and then I named the physical path as site\wwwroot and the Type as a Directory.
Now that I have created the directory, how do I get my txt file containing my ShoppingFeed on that directory? When I access the url Example.com/myvirtualdirectory I see that a page from Azure is up and running that says: "Hey, App Service developers! Your app service is up and running.Time to take the next step and deploy your code."
The purpose of this is so that when Google is scheduled to get my latest feed data, it comes to that url and is able to see all my product feed info.
So how do I get my file onto that location? Do I have to upload it the first time somewhere inside the Azure Portal ?
Do I have to create the directory as well on my actual application within visual studio? If so how do I do that? How do I create a virtual path or directory inside of my actual project if that is what I have to do?
How will this affect my deployments going forward now that there is a VD in the mix? Currently the existing application does not have virtual directories and I am deploying by clicking on "Publish" within visual studio..It deploys straight to the Azure App Service.

"Access to D:home\site\wwwroot is denied" ASP.NET MVC WebApp hosted on Azure

I've been trying to open an excel file in my ASP.NET MVC project (I use Syncfusion library for that).
For some reason, when I publish the webapp with Visual Studio, I get the
"Access to the path 'D:\home\site\wwwroot' is denied."
error.
The file is correctly uploaded to the webserver (I can download it just fine when I input the path in the browser). I've looked into this question, but there is no WEBSITE_RUN_FROM_PACKAGE setting in my azure webapp. I went into Kudu, opened up the console and ran
icacls "D:\home\site\wwwroot"
which gave this output
D:\home\site\wwwroot Everyone:(I)(OI)(CI)(M,DC)
BUILTIN\Administrators:(I)(OI)(CI)(F)
So, with my little understanding of acls, it seems that Everyone has Modify permission to this folder.
Which means that I'm at a loss as to what to do.
Code:
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
application.EnablePartialTrustCode = true;
IWorkbook workbook = application.Workbooks.Create(1);
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
dir = dir.Replace("file:\\", "");
dir = dir.Replace("\\bin", "");
var dirXlsx = "\\Areas\\Stampe\\Content\\Excel\\RapportoProvaMare(version1).xlsx";
FileStream s = File.OpenRead(dir + dirXlsx);
IWorkbook source = excelEngine.Excel.Workbooks.Open(s);
I don't think the Syncfusion code has anything to do with the issue, I'm just reporting it for completeness.
Anybody have an idea as to what the issue could be?
EDIT: To be more specific, I'm publishing the code via Visual Studio, with the excel file in a folder of the website (just like you would do for an image). All the test I've made to the dir variable have been tested on the Azure environment (we have a duplicate test environment). I just need to read the excel file from the local website folder and process it before sending it to the user.
Please use %HOME% for persistent file storage or %TMP% for temporal storage
You can create a %HOME% environment variable in your local computer for test purposes.

Setting file path for saved images in code, to correspond with hosting server location

Building web app in asp mvc3 with razor and sql server 2012.
I am stepping into new territory and my research has yet to come up with an answer. I am building a site that allows users to create a profile and upload an image. Of course I hope to have a ton of users so I am saving the images to file server, with a link in the db. However I have never built such a thing and I am trying to figure out the 'app to file server' relationship, specifically the path to the image location. Do I just create a file in my project folder and store them there? Or do I need to put in a temp path, then get the actual path from my host after deploy?
Currently i am storing in my solution:
[HttpPost]
public ActionResult ImageUpload(HttpPostedFileBase file)
{
string path = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(file.FileName));
file.SaveAs(path);
ViewBag.ImageUploadMessage = "File Uploaded Successfully";
return View();
}
This code works fine, assuming I have a folder in the root called 'Images', but this does not seem correct for a live site.
Rather creating a directory in your application you can also create a virtual directory on server and give its path to upload the file like this:
string path = "/VirtualDirecotryName/";
path = Path.Combine(Server.MapPath(path), fileName);
fileupload.SaveAs(path);
and also store the reference to the file in database. This is how I am managing user's profile pic in my application because it is very easy for me to maintain as files are being uploading on separate virtual directory not in application.
Hope this will help you.

Unable to upload image on mvc 4 web application hosted on windows azure

I have developed a web application using ASP.Net MVC 4, then hosted that web application on windows azure (windowsazure.com).
My website is unable to upload image/create file. Should I add some permissions?or how to add these permissions?
Please use Edit question to provide code snippet.
Give the code you've provided, the failing part is most probably the line:
Server.MapPath("~/UploadImages/" + ...);
What you really have to do, is first check whether that folder already exists:
string targetFolder = Server.MapPath("~/UploadImages");
if(!System.IO.Directory.Exists(taretfolder))
{
System.IO.Directory.Create(targetFolder);
}
// then copy the file here
The "problem", if one can say it is a problem at all, is that the server does not have this directory created when you try to put file into it. You have to first the directory before trying to copy files.
And by the way, it is not an "Azure" issue. If you take your code as is and upload it to a hoster (without manually creating or coping the UploadImages folder) you will encounter the very same issues.

File upload to IIS virtual directory

I am developing asp.net mvc web application in VS2008.In my project,users will upload their files.So that,we created a one folder and made virtual directory in IIS for it.That is just simple folder to store the files.But that folder path is not same with our application path and it is a separate folder.
For eg : upload folder path is "http://localhost/uploadfolder" and our application path is "http://localhost/TestApplication
I use Server.MapPath("~/uploadfolder") to get that upload virtual directory path but it returns like "http://localhost/TestApplication/uploadfolder" and I got error message "Upload directory not found". I also tried like Server.MapPath("http://localhost/uploadfolder") but still can't to upload.
My question is how can I point to that path from my application? How can I upload the files? I haven't hosted yet my application in IIS and I run it from Visual Studio.Please guide me the right way.I really appreciate your answer.

Resources