跳到主要內容

[HTML] 上傳檔案 drap and drop


[.html]

 <div id="divImg">
        <input id="Img" name="Img" type="file">                                  
         <img id="img" style="display:none;" src="" alt="圖片">
 </div>
             

[.js]

var files;

$("#Img").change(function () {
    if (this.files && this.files[0]) {
        FilePrepareUpload(this.files[0], this.files);
    }
});

$('#divImg').on('drop', function (e) {
    if (e.originalEvent.dataTransfer) {
        if (e.originalEvent.dataTransfer.files.length) {
            e.preventDefault();
            e.stopPropagation();
            FilePrepareUpload(e.originalEvent.dataTransfer.files[0], e.originalEvent.dataTransfer.files);
        }
    }
});

function updateProgress(evt) {
    if (evt.lengthComputable) {
        var percentLoaded = Math.round((evt.loaded / evt.total) * 100);      
        if (percentLoaded < 100) {
            $(".Progress").css("width", percentLoaded + '%');          
        }
    }
}

function FilePrepareUpload(originalFiles, dataFile) {
    var uploadErrors = [];
    var acceptFileTypes = /(\.|\/)(gif|jpe?g|png)$/i;
    if (originalFiles['type'].length && !acceptFileTypes.test(originalFiles['type'])) {
        uploadErrors.push("檔案格式錯誤,請上傳 .gif/.jpg/.jpeg/.png!");
    }
    if (originalFiles['size'] > 4096000) {
        uploadErrors.push("檔案太大!");
    }

    if (uploadErrors.length > 0) {
        //upload file error massege.
        alert(uploadErrors.join("\n"));
    } else {
         if (dataFile && dataFile[0]) {

            //process bar set 0%          

            var reader = new FileReader();
            reader.onprogress = updateProgress;
            reader.onload = function (e) {
                $("#img").attr("src", e.target.result);
                $("#img").css("display", "");

                //process bar set 100% 
            }
            reader.readAsDataURL(dataFile[0]);

            //close process bar.

            files = dataFile;
        }
    }
}

留言

這個網誌中的熱門文章

[python] python 時區轉換 ( timezone )

python 時區轉換 import datetime as dt import pytz d = dt.datetime(2019,1,1,14,0,0) #create Taipei timezone tw = pytz.timezone('Asia/Taipei') #set d timezone is 'Asia/Taipei' twdt = tw.localize(d) #change to utc time utc_dt = twdt.astimezone(pytz.utc) *注意 網路上很多使用 d.replace(tzinfo=tw)   但是可能會出現時差問題, 如下: datetime.time(23, 18, 5, tzinfo=<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>) 參考文件 python时区设置——pytz模块

[HTML] 文字浮水印效果 text watermark css

[.css] .info-mask {     height: 0;       position: absolute;       margin-top:120px;   } .info-opacity {     opacity: 0.8;     color: black; } .info-text {     color: gray;     font-size: 40px;     -webkit-transform: rotate(-5deg);       line-height: 60px;     max-width:1200px;     letter-spacing:3px;     padding-left:0.8em; } [.html]     <div class="info-mask">         <p class="info-text">             WaterMarkText         </p>     </div>    <table class="table table-bordered table-striped table-responsive table-hover" style="position:                 relative;" >        <tbody class="info-opacity" >     ...

[Jave] [GCP Dataflow] Pubsub To Firebase of Streaming

開發環境  Java  +  IntelliJ IDEA   Terminal  GCP Cloud SDK Firebase 登入GCP  - 開發專案如果權限沒有另外設定,依預設登入的google專案 ( Google Cloud SDK Shell ) ---2018/08/21 updated--- gcloud auth login gcloud auth application-default login gcloud config set project {projId} Deploy Dataflow ( Terminal ) 1.到專案的資料夾 cd {path} 2.deploy gardle (在專案的目錄下執行)  gradle run -Pargs="--project= {projId} --runner=DataflowRunner    --tempLocation=gs:// {bucket name} /temp --stagingLocation=gs:// {bucket name} /staging" 新增GCP專案 Real-time databse & 取得 GCP Auth http://beaminguna.blogspot.tw/2017/11/firebase-gcp-real-time-databse-gcp-auth.html 建立Pubsub -> 新增訂閱 -> 發布訊息 -> 接收訊息 ( Google Cloud SDK Shell ) gcloud init gcloud components install beta gcloud beta pubsub topics create myTopic gcloud beta pubsub subscriptions create --topic myTopic mySubscription gcloud beta pubsub topics publish myTopic "hello" gcloud beta pubsub subscripti...