为何二进制流提交到服务器,无法获取文件
自在修行 2012-11-11 08:03:24 做的一个裁剪图片后上传到服务器,服务器端使用的是asp.net
上传部分的代码是红字部分,能够发送网络连接,跟踪到提交时request.data有内容,但是服务器端无法获取文件内容
Flex程序文件如下:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="699" height="270" minWidth="955" minHeight="600"
>
<fx:Declarations>
</fx:Declarations>
<fx:Script>
<![CDATA[
//为裁剪框设置鼠标事件
//添加鼠标事件监听
import flash.net.FileReference;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.graphics.ImageSnapshot;
import mx.graphics.codec.*;
private var m_pictureBitmapData:BitmapData //定义视频截图
private var image_width:int=170; //图像框的厨师大小
private var image_height:int=190;
private var CurScale:Number=1//当前放大倍数
private var fileRef:FileReference = new FileReference();
private const defaultRequestUrl:String = "./zhaopian.aspx?action=post";
protected function Image_zoom1_clickHandler(event:MouseEvent):void
{
CurScale=CurScale+0.1;
image.scaleX=CurScale;
image.scaleY=CurScale;
image_back.scaleX=CurScale;
image_back.scaleY=CurScale;
//中心居中
Image_Move2Center();
}
protected function Image_zoom2_clickHandler(event:MouseEvent):void
{
if (CurScale > 1.0)
{
CurScale=CurScale-0.1;
}
image.scaleX=CurScale;
image.scaleY=CurScale;
image_back.scaleX=CurScale;
image_back.scaleY=CurScale;
Image_Move2Center();
}
//让图像居中显示
protected function Image_Move2Center():void
{
image.x=-(image_width*image.scaleX-image.width)/2;
image.y=-(image_height*image.scaleX-image.height)/2;
setImage_back();
//中心居中
}
//设置image_back的坐标与 image同步
protected function setImage_back():void
{
image_back.x=image.x+1;
image_back.y=image.y+1;
}
//移动图像
protected function imageMove(Drect:String):void
{
//移动时应该避免图片移出切图框
ss.text=image.width.toString();
var Pix:int=10;
switch (Drect)
{ case "left":
//右边不能超过
image.x=image.x-Pix;
break;
case "right":
image.x=image.x+Pix;
break;
case "up":
image.y=image.y-Pix;
break;
case "down":
image.y=image.y+Pix;
}
setImage_back()
}
//打开本地图片
protected function openfile_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
fileRef.addEventListener(Event.SELECT, selectHandler);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandle);
var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif;*.png");
var allTypes:Array = new Array(imageTypes);
//var textTypes:FileFilter = new FileFilter("Text Files (*.txt, *.rtf)", "*.txt; *.rtf");
//var allTypes:Array = new Array(imageTypes, textTypes);
fileRef.browse(allTypes);
}
private function selectHandler(evt:Event):void {
fileRef.load();
}
private function completeHandler(evt:Event):void {
image.source = fileRef.data;
image_back.source = fileRef.data;
}
private function progressHandle(event : ProgressEvent) : void {
ss.text="ok";
}
protected function upload_clickHandler(event:MouseEvent):void
{
var request : URLRequest = new URLRequest("./zhaopian.aspx");
fileRef.upload(request);
ss.text="uploading";
}
protected function button1_clickHandler(event:MouseEvent):void
{
var bd:BitmapData=ImageSnapshot.captureBitmapData(image_cut);
ImageSnapshot.defaultEncoder=JPEGEncoder;
//jpg编码
var jpgencode:JPEGEncoder = new JPEGEncoder();
//绘制后的位图图像 转为 JPEG格式的数据对象
//var jpegData:ByteArray = jpgencode.encode(bd);
new_img.source=bd;
var request:URLRequest = new URLRequest("http://localhost:51761/bin-release/zhaopian.aspx?action=post");
request.contentType = 'applicatoin/octet-stream';
request.method = URLRequestMethod.POST;
request.data = bd;
//返回接收数据
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.load(request);
loader.addEventListener(Event.COMPLETE, uploadcompleteHandler);
}
private function uploadcompleteHandler(event : Event):void{
Alert.show("上传完成");
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:BorderContainer id="k_back" x="10" y="0" width="233" height="270" borderVisible="false">
<s:BorderContainer x="30" y="37" width="170" height="190"
backgroundAlpha="0.34" backgroundColor="#A5A4A4" borderAlpha="1.0"
creationComplete="k_back.contentGroup.clipAndEnableScrolling = true">
<s:Image id="image_back" x="0" y="0" width="170" height="190" backgroundColor="#080808"
source="assets/0(1).jpg"/>
</s:BorderContainer>
</s:BorderContainer>
<s:BorderContainer id="k1" x="10" y="0" width="233" height="270" backgroundAlpha="0.45"
backgroundColor="#CCC9C9" borderAlpha="1.0" borderColor="#B6B4B4">
<s:BorderContainer id="image_cut" x="30" y="37" width="170" height="190"
backgroundAlpha="0.34" backgroundColor="#A5A4A4" borderAlpha="0.69"
borderColor="#FEFCFC" borderVisible="true"
creationComplete="image_cut.contentGroup.clipAndEnableScrolling = true">
<s:Image id="image" x="0" y="0" width="170" height="190" backgroundColor="#080808"
source="assets/0(1).jpg"/>
</s:BorderContainer>
</s:BorderContainer>
<s:Button x="252" y="230" width="32" label="+" click="Image_zoom1_clickHandler(event)"/>
<s:Button id="Image_zoom2" x="322" y="230" width="32" label="-" click="Image_zoom2_clickHandler(event)"/>
<s:Image id="new_img" x="488" y="10" width="201" height="241"/>
<s:Button id="upload" x="396" y="162" label="保存" click="upload_clickHandler(event)"/>
<s:Button id="M_up" x="284" y="151" width="32" label="↑" click="imageMove("up")"/>
<s:Button id="M_down" x="284" y="173" width="32" label="↓" click=" imageMove("down")"/>
<s:Button id="M_l" x="250" y="163" width="32" label="←" click="imageMove("left")"/>
<s:Button id="M_right" x="318" y="164" width="32" label="→" click=" imageMove("right")"/>
<s:Button id="openfile" x="252" y="41" width="80" height="30" label="本地文件"
click="openfile_clickHandler(event)" fontSize="14"/>
<s:Label id="ss" x="353" y="41" text="标签"/>
<s:Button x="396" y="109" label="按钮" click="button1_clickHandler(event)"/>
</s:Application>
服务器端程序 :
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Files As HttpFileCollection
Files = Request.Files
Dim FSPath As String
FSPath = "..\face\"
Dim AllowExt As String = "jpg;gif;bmp"
Dim UploadSize As Integer = 200 '单位k
Dim LimitSize As Integer = UploadSize * 1024
If Files.Count > 0 Then
Dim FullPath, Filename, FileSize, FileExt, SaveFile As String
FullPath = Files.Item(0).FileName
FileSize = Files.Item(0).ContentLength
Filename = "aaaa." & FileExt
SaveFile = Server.MapPath("\") & Filename
Files.Item(0).SaveAs(SaveFile)
End If
End Sub