手机WEB附件上传:html5版uploadify+webview上传附件实现

最近在做手机端web的一个邮件的附件上传功能,但是uploadify免费版本的是实现的是flash的上传的功能,html5版的是收费的。

网上找了一个根据flash版本改造的uploadify的html5版的插件huploadify实现。


Huploadify上传插件下载地址

jQuery文件上传插件,HTML5版uploadify,保持与uploadify一致的API

https://github.com/Double-Lv/Huploadify



<%@ include file="/common/huploadify.jsp"%>

<script type="text/javascript">
    $uploadify = $('#uploadify').Huploadify({
        auto:true,
        fileTypeExts:'*.*',
        multi:true,
        formData:{},
        buttonText:'上传附件',
        fileSizeLimit:102400,
        removeTimeout:9999999,
        fileObjName:'uploadFile',
        showUploadedPercent:true,
        showUploadedSize:true,
        uploader:ctxAdmin + '/mail/email/upload;jsessionid='+jsessionid,
        onUploadStart:function(file){
        },
        onInit:function(obj){
        },
        onUploadComplete:function(file, data){
            data = eval("(" + data + ")");
            if (data.code != undefined && data.code == "1") {
                fileIdArray.push(data.obj);
                var _fileIds = fileIdArray.join(",");
                $("#fileIds").val(_fileIds);
                dataMap.put(file.index,data.obj);
            } else {
                $('#' + file.id).find('.data').html(' - ' + "<font color=#D94600>" + data.msg + "</font>");
            }
        },
        onCancel:function(file){
            delUpload(dataMap.get(file['index']));
            dataMap.remove(file['index'])
        }
    });

/ * 删除附件 页面删除
 * @param fileId 后台File ID
 * @param uploadify
 */
function delUpload(fileId) {
    fileIdArray.splice($.inArray(fileId,fileIdArray),1)
    var _fileIds = fileIdArray.join(",");
    $("#fileIds").val(_fileIds);
    $.ajax({
        url: ctxAdmin + '/disk/delFolderFile',
        type: 'post',
        data: {fileIds: fileId},
        dataType: 'json',
        traditional: true,
        success: function(data) {
            if (data.code == 1) {
            }
        }
    });
}
</script>

<div id="uploadify"></div>



我的应用内嵌的是webview插件,相当于一个浏览器插件,不支持附件上传,点击上传不能弹出文件选择器,所以需要重写打开文件选择器(openFileChooser)方法

具体代码如下:


主要就是自定义了WebChromeClient

import android.webkit.ValueCallback;

public class BrowserActivity extends Activity {
    private WebView mWebView;
    private ValueCallback<Uri> mUploadMessage;
    private final static int FILECHOOSER_RESULTCODE = 1;

    public void onCreate(Bundle outState) {
        super.onCreate(outState);
        setContentView(R.layout.activity_browser);
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebChromeClient(new MyWebClient());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == mUploadMessage)
            return;
        Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
        }
    }

    public class MyWebClient extends WebChromeClient {
        // For Android 3.0-
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
        }

        // For Android 3.0+
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");
            startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
        }

        // For Android 4.1
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
        }
    }
}


完结。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值