CYQ.DataCYQ.Data09-02 01:48

手机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);
        }
    }
}


完结。。

程序之家二维码

小额赞赏

000
评论