服务端代码 aspx文件中
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpFileCollection uploadedFiles = Request.Files;
string Path = Server.MapPath("upload");
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile F = uploadedFiles[i];
if (uploadedFiles[i] != null && F.ContentLength > 0)
{
string newName = F.FileName.Substring(F.FileName.LastIndexOf("\\") + 1);
F.SaveAs(Path + "//" + newName);
}
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpFileCollection uploadedFiles = Request.Files;
string Path = Server.MapPath("upload");
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile F = uploadedFiles[i];
if (uploadedFiles[i] != null && F.ContentLength > 0)
{
string newName = F.FileName.Substring(F.FileName.LastIndexOf("\\") + 1);
F.SaveAs(Path + "//" + newName);
}
}
}
}
Flex代码 from FlexHelp
package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;
public class FileReferenceExample extends Sprite {
private var uploadURL:URLRequest;
private var file:FileReference;
public function FileReferenceExample() {
uploadURL = new URLRequest();
uploadURL.url = "http://localhost:8081/upload.aspx";
file = new FileReference();
configureListeners(file);
file.browse(getTypes());
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.CANCEL, cancelHandler);
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(Event.SELECT, selectHandler);
}
private function getTypes():Array {
var allTypes:Array = new Array(getImageTypeFilter(), getTextTypeFilter());
return allTypes;
}
private function getImageTypeFilter():FileFilter {
return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
}
private function getTextTypeFilter():FileFilter {
return new FileFilter("Text Files (*.txt, *.rtf)", "*.txt;*.rtf");
}
private function cancelHandler(event:Event):void {
trace("cancelHandler: " + event);
}
private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
trace("httpStatusHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
private function openHandler(event:Event):void {
trace("openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
var file:FileReference = FileReference(event.target);
trace("progressHandler name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
private function selectHandler(event:Event):void {
var file:FileReference = FileReference(event.target);
trace("selectHandler: name=" + file.name + " URL=" + uploadURL.url);
file.upload(uploadURL);
}
}
}
import flash.display.Sprite;
import flash.events.*;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;
public class FileReferenceExample extends Sprite {
private var uploadURL:URLRequest;
private var file:FileReference;
public function FileReferenceExample() {
uploadURL = new URLRequest();
uploadURL.url = "http://localhost:8081/upload.aspx";
file = new FileReference();
configureListeners(file);
file.browse(getTypes());
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.CANCEL, cancelHandler);
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(Event.SELECT, selectHandler);
}
private function getTypes():Array {
var allTypes:Array = new Array(getImageTypeFilter(), getTextTypeFilter());
return allTypes;
}
private function getImageTypeFilter():FileFilter {
return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
}
private function getTextTypeFilter():FileFilter {
return new FileFilter("Text Files (*.txt, *.rtf)", "*.txt;*.rtf");
}
private function cancelHandler(event:Event):void {
trace("cancelHandler: " + event);
}
private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
trace("httpStatusHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
private function openHandler(event:Event):void {
trace("openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
var file:FileReference = FileReference(event.target);
trace("progressHandler name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
private function selectHandler(event:Event):void {
var file:FileReference = FileReference(event.target);
trace("selectHandler: name=" + file.name + " URL=" + uploadURL.url);
file.upload(uploadURL);
}
}
}