代码改变世界

FileUploaderAJAX 无刷新上传

2009-11-07 20:38  曾祥展  阅读(4366)  评论(32编辑  收藏

http://en.fileuploadajax.subgurim.net/

  此控件支持如下功能:
1:多文件上传.
2:可以配置最大上传文件个数.
3.自定义js脚本.
4.支持删除已经上传的文件.
5.支持无刷新上传效果(iframe)
6.可配置相关提示文本

此控件是基于iframe方式实现的无刷新,开发人员并不需要开发iframe中的内容页,控件采用当前页来充当iframe的内容页.

using Subgurim.Controles;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // IsPosting可以标识是否触发上传事件.只有这种情况下才会触发保存文件事件,否则不会影响原页面的加载 .
        if (FileUploaderAJAX1.IsPosting)
        {
            this.managePost();  //一定要放在Page_Load事件中
        }   
    }

    private void managePost()
    {

        HttpPostedFileAJAX pf = FileUploaderAJAX1.PostedFile;
        if (pf.Type == HttpPostedFileAJAX.fileType.image)
        {
            FileUploaderAJAX1.SaveAs(Page.ResolveUrl("~/123/"), pf.FileName);
        }
        FileUploaderAJAX1.PostedFile.responseMessage_Uploaded_NotSaved = "<span style=\"font-family: arial; font-size: 12px;\">Sorry, there was a problem uploading this file.</span>";

    }
}

 

1

 

 

 

注册 引用

<%@ Register Assembly="FUA" Namespace="Subgurim.Controles" TagPrefix="cc1" %> 

<cc1:FileUploaderAJAX ID="FileUploaderAJAX1" runat="server" /> 

C#: using Subgurim.Controles;

VB.NET: Imports Subgurim.Controles 

 

参数:

FileName:
Name of the original file, as it is listed on the computer.

FileName_SavedAs:
Path (Directory + Name) that we have given to the file upon storing it to the system (applicable only when having done a Save).

FileName_Path:
Path formatted so that it is accessible by normal HTML.

ContentType:
The MIME type of the content of the file. Useful to filter what types of files we are going to admit. All of the examples are filtered to only

accept GIF images whose ContentType is "image/gif".

Type:
Is an enumerator "FileType" that assists ContentType to ascertain the type of file being dealt with. Basing on the ContentType (never the

extension of the file), allows us to distinguish the following types of files:

image: a file of any image: .gif, .jpg, .png, .bmp, etc.
audio: a file of any audio: .mp3, .wav, etc.
video: a file of general video: .mpg, .avi, etc.
text: a file that can be read like text. Can be flat text, an HTML file, a CSS file, an XML file, etc.
office: files used in the Office series of programs. (Word, Excel, PowerPoint, Access, Publisher, Project, etc.).
zip: ZIP compressed files.
application: Executable files that are not Office or ZIP files.
others: If the ContentType is known, but the file doesn’t match any other listed Type.
unknown: When the ContentType is not known.

ContentLength:
Length (in bytes) of the file. Also useful to limit maximum file size. All of the examples on this website prohibit files larger than 5KB

responseMessage_Uploaded_Saved:
Message that is displayed on screen when a file was successfully saved.

Defaults to:
"<a href="{3}" target="_blank">{0}</a> (>i>{1}>/i>) {2}KB".
Where:
{0} = FileName
{1} = ContentType
{2} = ContentLength
{3} = path for FileName_SavedAs

responseMessage_Uploaded_NotSaved:
Message that is displayed on screen when a file has not been properly stored.
Defaults to: "{0} is not saved." Where {0} = FileName

Saved: Boolean indicating whether the file has been stored correctly or not.
Deleted: Boolean indicating whether the file was deleted after you have uploaded.

responseMessage_Uploaded:
If there have been no changes, this is the message that the user sees after uploading the file. If the file was saved successfully display

“responseMessage_Uploaded_Saved”, otherwise “responseMessage_Uploaded_NotSaved”.

IsRequesting: indicates if a request due to FileUpload AJAX has been initiated. The request can be initiated when a file is uploading or

when it is being erased.

IsPosting: indicates that a file from the FileUpload AJAX control is uploading.

IsDeleting: indicates that a file is being deleted by FileUpload AJAX.

PostedFile: provides access to the HttpPostedFileAJAX that FileUpload AJAX uploaded.

Saving the PostedFile: we have 3 options when we have uploaded the PostedFile, none of which involve using "server.MapPath(...)",

because the control does it automatically:

SaveAs(string path): save the file to the specified path (directory + file name). The results are identical to the SaveAs command in the

standard FileUpload control.

Save(): save the file in the root of our application with the original name.

SaveAs(string directoryPath, string FileName): save the file in the specified directory (directoryPath) and with the specified name

(FileName). A function that is missing from the standard FileUpload control.