问题
I've made desktop app, and i want it to work also as an web app. I get errors on lines with File() type and FileStream, is there any way to bypass this ?
回答1:
To piggyback on @Florian F's correct response, here is how you might implement it:
Create an interface to abstract your file access
public interface IGetTheStuffService {
function getSomeTofu():Tofu;
}
Create a file-based implementation
public class FileStuffService implements IGetTheStuffService {
public function getSomeTofu():Tofu {
// File-based implementation
}
}
And an HTTP-based implementation
public class HTTPStuffService implements IGetTheStuffService {
public function getSomeTofu():Tofu {
// HTTP-based implementation
}
}
In your consumer, rely on the IGetTheStuffService
[Inject] public var tofuService:IGetTheStuffService;
public function doSomeStuff():void {
var tofu:Tofu = tofuService.getSomeTofu();
// act on the tofu
}
Notice the Inject meta tag. This is where your Dependency Injection (DI) system would push in your dependency based on the configuration (FileStuffService for Air apps or HTTPStuffService for Web apps). Parsley and RobotLegs use [Inject] where fiex-ioc uses [IocBind]
Of course, you could go without a DI container... you would just inject the parameter directly.
But you get the idea... a pretty simple pattern, actually.
回答2:
The File() class is only available for AIR applications, that's why you get the error.
This is how I would do it :
- You need to isolate platform dependant code and put in a separate library.
- Other code that work the same whatever the platform should be in another isolated library.
- Create 2 projects that will be simple wrappers. A Flex project that is only coupled to the common code. An AIR Project that is coupled to both common and platform dependant code.
You'll then probably need to use Interfaces at some time to switch between implementations that are platform specific
来源:https://stackoverflow.com/questions/4802489/file-filestream-in-web-app