1. 程式人生 > >javascript 圖片轉換base64

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();
                            }
                        }
                 }
            }); 
        }
    }