SwfUpload使用实例

作者: harde 分类: DotNet 发布时间: 2009-04-30 17:23

这篇文章是很久以前写的,写的很“雏”,大家见谅~~
大家不妨看看这篇文章:
SwfUpload的实际使用案例[PHP]:http://harde.org/blog/archives/1547

因为一个项目需要用到上传,客户要求需要看到上传进度

想想用Flash上传组件是最简单的,去SWFUpload网站看看

版本已经更新到了2.2

简单看了下说明文档,改动不大,就是增加了Flash 10的支持

下载demo看了看,自己试是着写了一个

 

我用C#写的,其他语言都差不多,自己更改吧

新建一个网站,建立JS(用来存放JavaScript脚本)、CSS、Upload(用来存放上传文件)

然后把fileprogress.js handlers.js swfupload.js swfupload.queue.js放入JS文件夹

(挨个文件夹翻翻,我用的是multiinstancedemo里的JS,各个文件夹里除了swfupload.js其他的文件根据需要都是不一样的……我懒得自己写了,(貌似我也不会写)就直接用现成的吧

 

打开Default.aspx

在head里写入下面的代码

[javascript” line=”1 ][/javascript” line=”1]

 

然后在Body中写入

<div id="swfu_container" style="margin: 0px 10px">
<div>
<div class="fieldset flash" id="fsUploadProgress">
				<span id="spanButtonPlaceholder"></span>
		    </div>
<div id="divFileProgressContainer" style="height: 75px"></div>
<input id="btnCancel" style="font-size: 8pt; margin-left: 2px; height: 22px" disabled onclick="cancelQueue(swfu);" type="button" value="取消上传" />
		    </div>
<div id="details"></div>
</div>

 

打开Default.aspx.cs

在PageLoad方法中写入

[C# ]Session.Clear();[/C#]

ok,主页面完成,下面写接受页面

新建一个Upload.aspx

因为我需要返回歌曲的地址,所以我把页面内容全删除了

只留下

[csharp” escaped=”true ]<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>[/csharp” escaped=”true]

然后在Upload.aspx.cs中写入

[csharp” escaped=”true ] if (Request.Files["Filedata"] == null)
{
Response.Redirect("Default.aspx");
}

Stream stream = null;//真是文件的流
Stream previewstream = null;//预览文件的流
BinaryReader br = null;//定义一个二进制读入流
BinaryWriter bw = null;//定义一个二进制写入流

HttpPostedFile uploadfile = Request.Files["Filedata"];
if (uploadfile != null)
{
DateTime dt = DateTime.Now;
String path = dt.ToString("yyyy\\MM\\dd");
String serverpath = Server.MapPath(".");

//GetNowTimeRan() 是我写的一个生成文件名的方法,这里就不写了,想要看看的可以留下您的Email
String realFile = serverpath + "\upload\" + path + "\" + GetNowTimeRan() + uploadfile.FileName.Substring(uploadfile.FileName.LastIndexOf("."));
String tempFileName = GetNowTimeRan() + uploadfile.FileName.Substring(uploadfile.FileName.LastIndexOf("."));
String previewFile = serverpath + "\preview\" + path + "\" + tempFileName;

//检查路径是否存在,不存在则创建
CreateDirectory(serverpath, path);

stream = uploadfile.InputStream;
previewstream = File.Create(previewFile);
br = new BinaryReader(stream);
bw = new BinaryWriter(previewstream);
int oldfilecount =Convert.ToInt32(stream.Length);
int count = 100000;

if (oldfilecount < count||count<10000)
{
count = 1000000;
}

if (uploadfile != null && uploadfile.ContentLength > 0)
{
//保存真实文件
uploadfile.SaveAs(realFile);

//保存预览文件
for (int j = 0; j < count; j++)//进行循环读取并添加到写入流中
{
bw.Write(br.ReadByte());
}

}

Response.Write("\Desktop1\preview\" + path + "\" + tempFileName);

bw.Close();
br.Close();
stream.Close();
stream.Dispose();
previewstream.Close();
previewstream.Dispose();

}[/csharp” escaped=”true]

OK,搞定,大家可以试着编译运行了。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

11 条评论
  • wsk

    2009-06-23 上午 10:34

    GetNowTimeRan()
    可不可以把这个方法给我发下
    呵呵,谢谢了

    1. harde

      2009-06-23 上午 10:45

      可以啊

      protected string GetNowTimeRan()
      {
      DateTime Time = DateTime.Now; //获取系统时间
      string Str = Time.Year.ToString();
      if (Time.Month < 10) Str += "0" + Time.Month.ToString(); else Str += Time.Month.ToString(); if (Time.Day < 10) Str += "0" + Time.Day.ToString(); else Str += Time.Day.ToString(); if (Time.Hour < 10) Str += "0" + Time.Hour.ToString(); else Str += Time.Hour.ToString(); if (Time.Minute < 10) Str += "0" + Time.Minute.ToString(); else Str += Time.Minute.ToString(); if (Time.Second < 10) Str += "0" + Time.Second.ToString(); else Str += Time.Second.ToString(); if (Time.Millisecond < 100) Str += "0" + Time.Millisecond.ToString(); else if (Time.Millisecond < 10) Str += "00" + Time.Millisecond.ToString(); else Str += Time.Millisecond.ToString(); //随机数字发生器 System.Random Random = new Random(); int _Temp; for (int i = 0; i < 3; i++) { _Temp = Random.Next(); _Temp = _Temp % 10; Str += _Temp.ToString(); } return Str; }

  • 2009-08-24 下午 4:09

    可以将CreateDirectory()方法发给我吗?

  • 2009-08-24 下午 4:16

    您可以发个完整事例给我吗?

    1. harde

      2009-08-29 上午 12:39

      貌似这个有点困难,年初硬盘挂了,这个项目已经没有了 …不过上面的问题还是可以解决的

      很简单的问题
      就是先检测下目标文件夹是否存在
      不存在就生成

      就这么简单啊

  • 2009-08-24 下午 4:28

    我按照恩的方法,报错

  • 2009-08-24 下午 4:30

    报fileDialogStart未定义,但是我导入达js文件啊

  • 小魏

    2010-09-26 上午 11:38

    不错的建议

  • zz

    2011-06-29 下午 9:46

    Microsoft JScript 运行时错误: ‘SWFUpload’ 未定义

    1. harde

      2011-07-18 下午 3:40

      确定引入js文件且无其他JS与其冲突?

  • zz

    2011-06-29 下午 9:47

    这个是什么问题?我用vs2008编的

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注