Flash XML config file problems with asp.net MVC - asp.net

I'm creating an asp.net MVC app, first time I've done this. I have a flash component I need to use in a view. I have included the SWF files etc in the Contents folder and referenced it from my view, the flash file loads when you get to the view, great.
The problem occurs because the flash file references and XML file for its configuration data, and I'm getting an error accessing that XML file. I'm guessing this is because flash is looking for a relative path and is using the URL for the page, which is obviously an MVC url and so does not refer to an actual location on disk, so the XML file is not there.
I guess the obvious answer is the alter the flash file to look in the contents folder for the XML file, but that means re-compiling the flash, and I know very little about flash so I'd like to avoid doing that. So is there any way to get the XML file to show up in the same URL as the view, so at the moment, the page with the flash component on is located at htttp://localhost/upload/ so I guess the XML file needs to be accessible from http://localhost/upload/flash-settings.xml?
If there's any other better way to do this, without editing the flash file, im open to that too,

Add this Action to the FlashUpload Controller:
public class FlashUploadController : Controller
{
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult FlashSettings()
{
var fileName = Server.MapPath("~/Contents/flash-settings.xml");
return new FilePathResult(fileName, "text/xml");
}
}
And this route to the RouteTable:
routes.MapRoute("FlashSettings", "upload/flash-settings.xml",
new { Controller = "FlashUpload", Action = "FlashSettings" });

You'll need to either set the routing mechanism to allow for direct files access to the /upload/ folder, or create a Controller Action which will return an XML stream (dynamic or the one read from the physical XML file), and point your SWF to that Route. I'd go with the second option, as it is much flexible.

Related

Access static files on server from RCL

I've read dozens of questions but none seem to work for me. I'm not using IIS. I'm using ASP.Net Core 3.1 with Kestrel.
I have a Razor Class Library with a resources folder, in that I have folders like css, js, content, etc. I use this library so all of my common Web Api projects can share common files instead of duplicating them everywhere. To do this, I followed the guide from here. Please see there for how Startup is configured.
That works great, I can access those files by going to e.g. localhost/css/site.css. In my cshtml files, I can include that file by doing <link rel="stylesheet" href="~/css/site.css"/>
The problem arises when I try to access those files from a controller. I have a sister folder to css called content which contains json files. I can view that file by going to localhost/content/test.json, but I can't figure out how to access it from a controller.
What I'd like to do is make an HTTP request to ~/content/test.json and download its contents, but the path is not found.
I've tried using Url.Content to map a relative path to the absolute path, but it doesn't work.
var url = Url.Content("~/content/test.json"); // spits out "/content/test.json"
I've DI'd IWebHostEnvironment into the controller and tried to access the ContentRootFileProvider and the ContentRootPath and WebRootPath, but those paths aren't right either. They are pointing to my currently running service's wwwroot's parent and wwwroot, respectively.
I've tried creating my own file provider:
var filesProvider = new ManifestEmbeddedFileProvider(GetType().Assembly, "resources");
var content = filesProvider.GetDirectoryContents("content");
var fileInfo = filesProvider.GetFileInfo(Path.Combine("content", "test.json"));
This successfully finds the file and claims it exists, but the fileInfo's PhysicalPath is always null.
I just want to do something like this:
string SomeMagicMapFunction(string s) => ????
var webClient = new WebClient();
var json = webClient.DownloadString(SomeMagicMapFunction("~/content/test.json"));
Where am I going wrong? Any pointers are appreciated.

Force download audio file in ASP.NET using public path

does any one knows how to force the downloa of a file with the "...Save As" window using a public path, for example:
www.domain.com/audio/myfile.mp3
I have found a lot of examples but using a virtual path and noth a public path.
Thank you
You might be able to do some IIS url rewrite voodoo to pull this off. You could easily do some URL routing to make it look like you are downloading from /audio/file.mp3 but are actually pulling the file through a special handler.
But if you are just serving a file with a mimetype there isn't a whole lot you could do -- the client really controls how they handle files when not instructed and you've got no instructions on how to handle it if you are just posting files for download in a directly accessible manner.

ASP.NET MVC - Non-Embedded Resource FIle

I currently have a local resource file in an ASP.NET MVC project. The goal of this file is to let a non-programmer edit the text on the webpage. The problem is that the resource file is an embedded resource and compiled on ASP.NET MVC Deployment. This means they would have visual studio to change site copy. This is non-optimal :)
Is there a way to make use of resource files in ASP.NET MVC that would result in a .resx file on the server for an admin-type person to be able to edit?
The other option is to put this in a database and have some front end to edit it, but I would really like to avoid this option as its overly complex for just a few text fields on a small site.
Thanks!
Once way of doing so, is to make sure the resources aren't compiled.
When you add a resource file (e.g. TextResource.resx) you can access the resources in a type safe manner.
For example:
ViewBag.Message = Resources.TextResource.MyMessage;
After you add a resource file (*.resx), select it in the solution explorer and view its properties. Clear the "Custom Tool" property. By default it contains the value "GlobalResourceProxyGenerator". Just delete this value.
This has a downside, however as you can no longer access the resources in a type-safe manner. You'll have to load them using the GetGlobalResourceObject and GetLocalResourceObject methods of the current HttpContext.
For example:
ViewBag.Message = this.HttpContext.GetGlobalResourceObject("TextResource",
"Hello", CultureInfo.CurrentUICulture);
This way you'll be able to manually alter the resource files (*.resx). You could then opt to build an application which can read and modify the contents of the resource files, which are just XML files.
For example:
http://www.codeproject.com/KB/cs/Editing_a_ResourceFile.aspx

File browser control to replace <input type="file">

I'm working on an ASP.NET web application for our corporate intranet users. I have a form where a user should provide a path to the file on the local network (something like "\localServer\someFolder\someFile.ext") without uploading the actual file. The issue is that users don't want to type the whole file path and want to use some kind of visual browse dialog.
The standard HTML <input type=file> element allows to browse for a file, but most of the browsers (except for IE) don't allow to access file's full path, so I think it should be done by some external component like Silverlight, Flash, Java applet etc.
I tried to do it with Silverlight, but I'm getting a SecurityException when trying to access file's full path using Silverlight's OpenFileDialog class.
This java applet http://jumploader.com/demo_images.html seems to do something similar to what I'm looking for, but it's focused on uploading files - I only need to be able to get file's full path and pass it to the server as a string.
Any suggestions would be appreciated.
Telerik ASP.NET AJAX RadFileExplorer has the functionality you're looking for:
http://www.telerik.com/products/aspnet-ajax/fileexplorer.aspx
You can use their Custom File Content Provider to hook the GUI to your server's file system.
http://demos.telerik.com/aspnet-ajax/fileexplorer/examples/server-sideapi/dbfilebrowsercontentprovider/defaultcs.aspx
This should be possible with Flash's uploading capabilities. SWFUpload has an API that you may be able to access from JavaScript to extract the selected file name without actually uploading anything. See docs here, for example getFile():
getFile is used to retrieve a File Object from the queue. The file retrieved by passing in a file id (the id property from a file object) or a file index (the index property from a file object).

How to load WSDL from file

I am trying to save some bandwidth and include wsdl file in my flex/air application. Which url format should I use in order to load that file instead of the remote one.
I am using loadWSDL() method.
EDIT:
wsdl file needs to be part of the application. I know I can use file://some/path for local files, but don't know how to load file which is inside application itself.
If the file is local, just use the file URI scheme:
file://host/path/file.wsdl
If this doesn't work, check if the security sandbox features are blocking it.
In AIR apps, in order to access files in the application's temporary storage directory or the application's own directory, you need to use special app: or app-storage: URL schemes, though.
Like dirkgently said, you can always embed the file into the application, but as far as I know, you then won't be able to modify it afterwards in a persistent manner since it's not just a file in the filesystem. Probably the best option for you is to embed this file and if you later need to update it, have the app save an updated version into the File.applicationStorageDirectory (which you would then always check first before using the default embedded version.) Although I have no idea if using embedded XML files with the WebService classes is even possible.
See this article for info on how to embed external XML files into your app. This is how I've done it:
// note: common sense says that the mimeType should be "text/xml" here but
// it doesn't work -- this does, though. who knows why.
[Embed(source="File.xml", mimeType="application/octet-stream")]
private const _fileXMLClass:Class;
private var _fileXML:XML = XML(new _fileXMLClass());
wsdl file needs to be part of the application.
Have you tried embedding it inside the Flex/AIR project as a resource? Read this. For example, you can load static images shipped with your app by specifying the source as:
source="#Embed(source='relativeOrAbsolutePath')"
Uf, this was ugly, so I'm answering for the reference. Thanks for insights to hasseg and dirkgently
Here is the code.
First, declare the variables:
[Embed(source="/ws/wsdl/LoginService.wsdl",
mimeType="application/octet-stream")]
private const _fileXMLClass:Class;
private var _fileXML:XML = XML(new _fileXMLClass());
Then, loading wsdl:
var file : File = dir.resolvePath(name + ".xml");
var stream : FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes(getWsdl().toXMLString());
stream.close();
loadWSDL(file.url);
If someone have an idea to make this less ugly, please let me know.
EDIT: I just noticed edited answer, so instead of this code it was enough to use just:
loadWSDL('app:///path/to/my/file.wsdl');
I use below code in flash builder air mobile app and it works, may help some else. I get file contents from a web service using url loader and wirte it down to a xml file in document directory of my air app.
var url:URLRequest = new URLRequest(Globals.deviceSettings.endpoint);
loader.load(url);
loader.addEventListener(Event.COMPLETE, loaderComplete);
get the status of web service, if it is 200 then available and heads up.
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, ldrStatus);
and in the eventlistener
function loaderComplete(e:Event):void
{
var f:File= File.documentsDirectory.resolvePath("source/category.xml");
var _xml:XML = new XML(loader.data);
var fs:FileStream = new FileStream();
fs.open(f, FileMode.WRITE);
fs.writeUTFBytes(_xml.toXMLString());
fs.close();
popup.close(true);
var popup:MyPopupComponent = new MyPopupComponent();
popup.show("Successfully updated from the server",this);
popup.close();
}
you can use file.documentdirectory or applicaiton or your choice directory as per your need please keep in mind that some of the paths are read only for security. if you want to write back to those files you wont be able but just for reading purposes it is a good idea to place the files there.

Resources