webブラウザでアップロードされたファイルをシェルのみで処理する
まずは、html
<INPUT TYPE="file" id="sound_file_0" style="width:240px;" NAME="sound_file_0" VALUE=""> <INPUT style="text-align:center" TYPE="button" id="menu4_sound_0" VALUE="Upload" onClick="return menu4_ck('menu4_sound_0','disp_sound_0');"/> <INPUT style="text-align:center" TYPE="reset" VALUE="Clear">つぎに、javascript(jquery)でuploadファイルとサイズを取得、非同期でpost
function menu4_ck(button_id,disp_id) { /* upload sound file & check */ var check = 0; var error_ct = 0; var call_cgi = "none"; var file_name = "none"; var formdata = new FormData($('#menu4').get(0)); switch (button_id) { case 'menu4_jikkou': call_cgi = 'sound_set.cgi'; break; case 'menu4_sound_0': file_name = document.menu4.sound_file_0.value; if (file_name != ""){ disp_id = disp_sound_0; call_cgi = 'sound_upload.cgi'; formdata.append('size',$('#sound_file_0')[0].files[0].size); formdata.append('sound_file_0',$('#sound_file_0')[0].files[0]); } else { return false; } break; case 'menu4_sound_1': . .中略 . if (call_cgi != "none") { $.ajax({ type: "POST", url: call_cgi, timeout : 10000, dataType: "html", data: formdata, cache : false, contentType : false, processData : false, success: function() { $(disp_id).text("File Upload Success!"); return; }, error: function(){ $(disp_id).text("File Upload faile!"); return; } }); }cgi、webサーバー側シェルスクリプトでブラウザがpostしたデータに付加された
ヘッダーとトレーラーを分離・解析、uploadされたファイルの取り出しを行う
ファイルはここhttp://www.bird-research.jp/1_shiryo/nakigoe.htmlからダウンロード使用させて頂きましたm(_ _)m
参考にpostデータのdump
# hexdump -C .sound_file_tmp |less
# hexdump -C .sound_file_tmp |tail |less
00000000 2d 2d 2d 2d 2d 2d 57 65 62 4b 69 74 46 6f 72 6d |------WebKitForm| 00000010 42 6f 75 6e 64 61 72 79 6b 42 47 6b 58 79 48 47 |BoundarykBGkXyHG| 00000020 4b 41 37 35 36 71 31 5a 0d 0a 43 6f 6e 74 65 6e |KA756q1Z..Conten| 00000030 74 2d 44 69 73 70 6f 73 69 74 69 6f 6e 3a 20 66 |t-Disposition: f| 00000040 6f 72 6d 2d 64 61 74 61 3b 20 6e 61 6d 65 3d 22 |orm-data; name="| 00000050 73 69 7a 65 22 0d 0a 0d 0a 39 36 34 31 32 0d 0a |size"....96412..| 00000060 2d 2d 2d 2d 2d 2d 57 65 62 4b 69 74 46 6f 72 6d |------WebKitForm| 00000070 42 6f 75 6e 64 61 72 79 6b 42 47 6b 58 79 48 47 |BoundarykBGkXyHG| 00000080 4b 41 37 35 36 71 31 5a 0d 0a 43 6f 6e 74 65 6e |KA756q1Z..Conten| 00000090 74 2d 44 69 73 70 6f 73 69 74 69 6f 6e 3a 20 66 |t-Disposition: f| 000000a0 6f 72 6d 2d 64 61 74 61 3b 20 6e 61 6d 65 3d 22 |orm-data; name="| 000000b0 73 6f 75 6e 64 5f 66 69 6c 65 5f 34 22 3b 20 66 |sound_file_4"; f| 000000c0 69 6c 65 6e 61 6d 65 3d 22 79 6f 74 61 6b 61 5f |ilename="yotaka_| 000000d0 30 37 30 35 32 31 5f 6e 69 6b 6b 6f 5f 6d 6a 75 |070521_nikko_mju| 000000e0 2e 6d 70 33 22 0d 0a 43 6f 6e 74 65 6e 74 2d 54 |.mp3"..Content-T| 000000f0 79 70 65 3a 20 61 75 64 69 6f 2f 6d 70 33 0d 0a |ype: audio/mp3.. 以下アップロードファイルの実体 00000100 0d 0a 49 44 33 03 00 00 00 00 2e 62 54 59 45 52 |..ID3......bTYER| 00000110 00 00 00 0d 00 00 01 ff fe 32 00 30 00 31 00 34 |.........2.0.1.4| 00000120 00 00 00 54 44 41 54 00 00 00 0d 00 00 01 ff fe |...TDAT.........| . .中略 . 000177f0 78 60 ff 80 0c 60 00 46 a0 00 00 0a 00 00 09 70 |x`...`.F.......p| 00017800 00 00 01 00 00 01 2e 00 00 00 20 00 00 25 c0 00 |.......... ..%..| 00017810 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00017820 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 00017990 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0d 0a |................| 000179a0 2d 2d 2d 2d 2d 2d 57 65 62 4b 69 74 46 6f 72 6d |------WebKitForm| 000179b0 42 6f 75 6e 64 61 72 79 6b 42 47 6b 58 79 48 47 |BoundarykBGkXyHG| 000179c0 4b 41 37 35 36 71 31 5a 2d 2d 0d 0a |KA756q1Z--..| 000179ccupload原本ファイルのdump
root@tmp# hexdump -C yotaka_070521_nikko_mju.mp3 |less
root@tmp# hexdump -C yotaka_070521_nikko_mju.mp3 |tail | less
00000000 49 44 33 03 00 00 00 00 2e 62 54 59 45 52 00 00 |ID3......bTYER..| 00000010 00 0d 00 00 01 ff fe 32 00 30 00 31 00 34 00 00 |.......2.0.1.4..| 00000020 00 54 44 41 54 00 00 00 0d 00 00 01 ff fe 30 00 |.TDAT.........0.| 00000030 34 00 30 00 36 00 00 00 54 49 4d 45 00 00 00 0d |4.0.6...TIME....| 00000040 00 00 01 ff fe 31 00 36 00 32 00 34 00 00 00 50 |.....1.6.2.4...P| 00000050 52 49 56 00 00 0f 13 00 00 58 4d 50 00 3c 3f 78 |RIV......XMP.<?x| 00000060 70 61 63 6b 65 74 20 62 65 67 69 6e 3d 22 ef bb |packet begin="..| 00000070 bf 22 20 69 64 3d 22 57 35 4d 30 4d 70 43 65 68 |." id="W5M0MpCeh| 00000080 69 48 7a 72 65 53 7a 4e 54 63 7a 6b 63 39 64 22 |iHzreSzNTczkc9d"| 00000090 3f 3e 0a 3c 78 3a 78 6d 70 6d 65 74 61 20 78 6d |?>.<x:xmpmeta xm| 000000a0 6c 6e 73 3a 78 3d 22 61 64 6f 62 65 3a 6e 73 3a |lns:x="adobe:ns:| 000000b0 6d 65 74 61 2f 22 20 78 3a 78 6d 70 74 6b 3d 22 |meta/" x:xmptk="| 000000c0 41 64 6f 62 65 20 58 4d 50 20 43 6f 72 65 20 35 |Adobe XMP Core 5| 000000d0 2e 32 2d 63 30 30 33 20 36 31 2e 31 34 31 39 38 |.2-c003 61.14198| 000000e0 37 2c 20 32 30 31 31 2f 30 32 2f 32 32 2d 31 32 |7, 2011/02/22-12| 000000f0 3a 30 33 3a 35 31 20 20 20 20 20 20 20 20 22 3e |:03:51 ">| 00000100 0a 20 3c 72 64 66 3a 52 44 46 20 78 6d 6c 6e 73 |. <rdf:RDF xmlns| 00000110 3a 72 64 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77 |:rdf="http://www| 00000120 2e 77 33 2e 6f 72 67 2f 31 39 39 39 2f 30 32 2f |.w3.org/1999/02/| 00000130 32 32 2d 72 64 66 2d 73 79 6e 74 61 78 2d 6e 73 |22-rdf-syntax-ns| 00000140 23 22 3e 0a 20 20 3c 72 64 66 3a 44 65 73 63 72 |#">. <rdf:Descr| 00000150 69 70 74 69 6f 6e 20 72 64 66 3a 61 62 6f 75 74 |iption rdf:about| 00000160 3d 22 22 0a 20 20 20 20 78 6d 6c 6e 73 3a 78 6d |= . .中略 . 00017670 ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00 00 |................| 00017680 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 000176e0 00 00 00 00 00 00 00 00 00 00 00 00 ff fb 78 60 |..............x`| 000176f0 ff 80 0c 60 00 46 a0 00 00 0a 00 00 09 70 00 00 |...`.F.......p..| 00017700 01 00 00 01 2e 00 00 00 20 00 00 25 c0 00 00 04 |........ ..%....| 00017710 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| *cgi、シェルスクリプト
DIR=/www/remote-hand/tmp FILE_NAME=$DIR/.sound_file_name tFILE_NAME=$DIR/.sound_file_name_tmp SOUND_FILE=$DIR/.sound_file tSOUND_FILE=$DIR/.sound_file_tmp cat >$tSOUND_FILE cat $tSOUND_FILE | sed -n 6,6p|awk '{gsub("\r","",$0);gsub(";","",$0);printf("%s\n%s\n",$3,$4)}' >$tFILE_NAME . $tFILE_NAME SIZE=`cat $tSOUND_FILE | awk 'NR == 4{gsub("\r","",$0);printf $1}'` case $name in "sound_file_0") tmp="sound_file[0]" ;; "sound_file_1") tmp="sound_file[1]" . .中略 . esac if [ -e $FILE_NAME ];then cat $FILE_NAME |grep -v $tmp >$tFILE_NAME echo "$tmp"="$filename" >> $tFILE_NAME mv $tFILE_NAME $FILE_NAME else echo "$tmp"="$filename" > $FILE_NAME fi FILE=$DIR/$filename cat $tSOUND_FILE | sed '1,8d' >$SOUND_FILE dd if=$SOUND_FILE of=$FILE bs=1 count=$SIZE
[ページ情報]
更新日時: 2017-07-12 05:59:53, 更新者: pepolinux
[ライセンス]
GNU Free Documentation License
[権限]
表示:無制限, 編集:文書編集者, 削除/設定:文書編集者