天天看點

javascript 圖檔轉換base64

if ( typeof(FileReader) === 'undefined' ){  
        alert("抱歉,你的浏覽器不支援圖檔上傳功能,請使用最新浏覽器操作");
    } else {
        var e = document.getElementById(name);
        var fileList = e.files;
        var file = fileList[0];
        alert(file.type);
        if (!/image\/\w+/.test(file.type)) {
            alert("請確定檔案為圖像類型");
        } else {
            var reader = new FileReader();
            reader.readAsDataURL(file); 
            var aaa=  reader.result; 
                $.ajax({
                    type: "post",
                    url: "/CallReport/ApiSaveCallReportMedia",
                    data: { "SurveyId": ASurveyId, "PeriodId": APeriodId, "StoreId": AStoreid, "SurveyQuestionId": QuestionId, "Tag": "", "Latitude": latitude, "Longitude": longitude, "FileStream": aaa },
                    dataType: "json",
                    success: function (msg) {
                        var json = eval('(' + msg + ')');
                        if (json.HasError) {

                        } else {
                            if (json.Value == "success") {
                                $("#photo" + callReportAnswerId).remove();
                            }
                        }
                 }
            }); 
        }
    }
           

繼續閱讀