如何将文件转换为字节? [英] How to convert File into byte?
本文介绍了如何将文件转换为字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将文件转换为字节.....我正在使用它进行文件上传,其中我正在获取文件的字节并调用我在c Sharp中的上传处理程序并上传它..任何人都可以帮忙.. ...
Hi,
I want to convert file into bytes..... i am using this for file upload in which i am taking bytes of the file and calling my upload handler which is in c sharp and uploading it.... can anyone help.....
推荐答案
我认为您可以执行类似的操作来获取字节数组
I think you can do something like this to fetch the byte array
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="loadFile()">
<mx:Script>
<![CDATA[
private var loadedFile:ByteArray;
private function loadFile():void
{
var request:URLRequest = new URLRequest("http://test.enunciato.org/sample.pdf");
var urlLoader:URLLoader = new URLLoader(request);
urlLoader.addEventListener(Event.COMPLETE, onURLLoaderComplete);
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(request);
}
private function onURLLoaderComplete(event:Event):void
{
loadedFile = event.target.data;
}
private function saveLoadedFile():void
{
var file:FileReference = new FileReference();
file.save(loadedFile, "SampleDoc.PDF");
}
]]>
</mx:Script>
<mx:Button label="Save Image" horizontalCenter="0" verticalCenter="0" click="saveLoadedFile()" />
</mx:Application>
在这里看看,关于同一主题还有另一个问题:
如何将pdf文件转换为字节数组,检索将字节数组转换为Flex中的pdf文件? [
take a look here, there was just another question about the same topic:
How to convert pdf file into byte array,retrieve byte array into pdf file in flex?[^]
我使用了以下代码,对我来说效果很好....它将在已安装Flash Player 10,并且根据以下站点进行了更改设置:
I have used following code and worked fine for me ....and it will work when flash player 10 is installed and settings are changed as per below site:
FileRef.addEventListener(Event.SELECT,FileSelect);
FileRef.addEventListener(Event.COMPLETE,onFileComplete);
var filedata:ByteArray=new ByteArray();
public function onFileComplete(event : Event) : void
{
FileRef = event.currentTarget as FileReference;
filedata=FileRef.data;
var data:ByteArray = new ByteArray();
FileRef.data.readBytes(data,0,FileRef.data.length);
// byteCheckUpload.writeLocation(data, FileRef.name)
}
private function FileSelect(evt:Event):void
{
FileRef.load();
Txt_FileName.text=evt.currentTarget.name;
}
http://opensource.adobe.com/wiki/display/flexsdk/Targeting+Flash+ Player + 10 [ ^ ]
http://opensource.adobe.com/wiki/display/flexsdk/Targeting+Flash+Player+10[^]
这篇关于如何将文件转换为字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文