异步文件上传控件

发布于 2024-08-08 02:16:01 字数 183 浏览 5 评论 0原文

我正在使用最新 AjaxControl ToolKit 中的新 AsyncFileUpload 控件。 我的查询是关于在上传开始之前触发的 OnClientUploadStarted 事件。有什么方法可以取消上传,因为我此时正在检查文件扩展名,并且想取消上传,以便它不会继续并继续上传文件。我的最终结果是只允许上传图像。 请提供建议并感谢您的宝贵时间。

I am using the new AsyncFileUpload control from the latest AjaxControl ToolKit.
My query is regarding the OnClientUploadStarted event which is fired before the upload is started. Is there any way to cancel the upload, as I am checking the fileExtension at this point and would like to cancel the upload so that it does not continue and go on to upload the file. My end result is allow only images to be uploaded.
Please advise and thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愿与i 2024-08-15 02:16:01

得到了答案,我所要做的就是用这个脚本覆盖 javascript 函数(不是最好的答案,但有效),你们都可以做得更快更干净

var orig = AjaxControlToolkit.AsyncFileUpload.prototype.raiseUploadStarted;
AjaxControlToolkit.AsyncFileUpload.prototype.raiseUploadStarted = function(e) {
var evt = this.get_events()._getEvent('uploadStarted');
if (evt) {
    if (evt.length > 1)
        return orig(e);
    else if (evt.length === 1)
        return evt[0](this, e);
    }
}

Got the answer, all I had to do was override the javascript function with this script(not the best answer, but works), you all could have done faster and cleaner

var orig = AjaxControlToolkit.AsyncFileUpload.prototype.raiseUploadStarted;
AjaxControlToolkit.AsyncFileUpload.prototype.raiseUploadStarted = function(e) {
var evt = this.get_events()._getEvent('uploadStarted');
if (evt) {
    if (evt.length > 1)
        return orig(e);
    else if (evt.length === 1)
        return evt[0](this, e);
    }
}
爱已欠费 2024-08-15 02:16:01

您可以尝试在该字段中添加“正则表达式验证器”,并查看是否可以使用它来验证在上传开始之前选择的文件。

You might try adding a "Regular Expression Validator" to the field, and see if you can use that to validate the file selected before the upload starts.

妄司 2024-08-15 02:16:01

试试这个代码:

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            if (rdoFileType.SelectedValue == String.Empty)
            {
                lblStatus.Text = "Please select a file type before selecting a file.";
                AsyncFileUpload1.FailedValidation = true;
                e.state = AjaxControlToolkit.AsyncFileUploadState.Failed;
                return;
            }

            try
            {
                // System.Threading.Thread.Sleep(5000);
                if (AsyncFileUpload1.HasFile)
                {
                    string _filename = System.IO.Path.GetFileName(e.filename);


                    System.IO.FileInfo f = new System.IO.FileInfo(AsyncFileUpload1.PostedFile.FileName);

                    if (rdoFileType.SelectedValue == "F")
                    {
                        if (f.Extension != ".pdf")
                        {
                            lblStatus.Text = "Final Document must be a .pdf";
                            e.state = AjaxControlToolkit.AsyncFileUploadState.Failed;
                            e.statusMessage = "Final Document must be a .pdf";
                            throw new Exception("Final Document must be a .pdf");

                        }

Try this code:

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            if (rdoFileType.SelectedValue == String.Empty)
            {
                lblStatus.Text = "Please select a file type before selecting a file.";
                AsyncFileUpload1.FailedValidation = true;
                e.state = AjaxControlToolkit.AsyncFileUploadState.Failed;
                return;
            }

            try
            {
                // System.Threading.Thread.Sleep(5000);
                if (AsyncFileUpload1.HasFile)
                {
                    string _filename = System.IO.Path.GetFileName(e.filename);


                    System.IO.FileInfo f = new System.IO.FileInfo(AsyncFileUpload1.PostedFile.FileName);

                    if (rdoFileType.SelectedValue == "F")
                    {
                        if (f.Extension != ".pdf")
                        {
                            lblStatus.Text = "Final Document must be a .pdf";
                            e.state = AjaxControlToolkit.AsyncFileUploadState.Failed;
                            e.statusMessage = "Final Document must be a .pdf";
                            throw new Exception("Final Document must be a .pdf");

                        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文