AppCan 文件上传实例

前台代码:

<!DOCTYPE html>
<html class="ui-mobile landscape min-width-240px min-width-320px min-width-480px min-width-768px min-width-1024px">
  <head>
    <title>
    </title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="stylesheet" href="css/ui-media.css">
    <link rel="stylesheet" href="css/ui-base.css">
    <link rel="stylesheet" href="css/ui-apple.css">
    <script src="js/zy_anim.js">
    </script>
    <script src="js/zy_control.js">
    </script>
     <script src="js/zy_tmpl.js">
    </script>
  </head>
  <body class="ui-mobile-viewport" >
   
<div id="page" class="ui-page ui-body-d ui-page-active ui-header-fixed" tabindex="0">

<div data-role="header" class="ui-header ui-bar-iphone " >
<h1 class="ui-title" tabindex="0" role="heading" aria-level="1">Images</h1> 
<div class="ui-btn  ui-btn-right ui-btn-corner-all ui-shadow  ui-btn-a  " onclick="actionsheet();">  <span class="ui-btn-inner ui-btn-corner-all"> <span class="ui-btn-text">选择</span>  </span></div> 

</div>

<div id="msgid" class="ui-bar ui-bar-e ">
</div>

<div class="ui-content ui-body-d">
<img id="imgid" src="img/2.png" style="height:500px;width:450px;position:absolute;left:0.5em;"/> 
</div>

<div data-role="footer" class="ui-footer ui-bar-a ui-footer-fixed">
<div class="ui-bar ui-bar-a ">
<div class="ui-btn   ui-btn-corner-all ui-shadow  ui-btn-a ui-btn-inline " onclick="viewpic();">  <span class="ui-btn-inner ui-btn-corner-all"> <span class="ui-btn-text">图片查看器</span>  </span> <button class="ui-btn-hidden">按钮</button> </div> 
<div class="ui-btn   ui-btn-corner-all ui-shadow  ui-btn-a ui-btn-inline " onclick="downloadurl();">  <span class="ui-btn-inner ui-btn-corner-all"> <span class="ui-btn-text">下载网络图片</span>  </span> <button class="ui-btn-hidden">按钮</button> </div> 
<div class="ui-btn   ui-btn-corner-all ui-shadow  ui-btn-a ui-btn-inline " onclick="uploadpic();">  <span class="ui-btn-inner ui-btn-corner-all"> <span class="ui-btn-text">上传</span>  </span> <button class="ui-btn-hidden">按钮</button> </div> 

</div>


</div>

</div>
<script>
var imgurl = "wgt://img/2.png";//默认图片url

//显示log
function setLog(msg){
    document.getElementById("msgid").innerHTML = msg;
}

//弹出actionSheet
function actionsheet(){
    uexWindow.actionSheet("选择图片", "取消", ["照相机拍摄","本地文件","网络文件"]);
}
//通过图片查看器查看图片
function viewpic(){
    var ar = new Array();
    ar[0]=imgurl;
    uexImageBrowser.open(ar);
}

//下载网络图片
var dopCode=1;
var inSavePath=null;
function downloadurl(){
    if(imgurl.indexOf("http://")==0){
        var q = imgurl.match(/.*\.(.*)/);
        inSavePath = (new Date()).getTime()+"."+q[1];
        uexDownloaderMgr.createDownloader(dopCode); 
    }else{
        uexWindow.toast(0,5,"非网络图片",1000);
    }
}
//上传图片
var uploadHttp = "http://192.168.1.23:86/abc.ashx?from=web";
var uopCode =2;
function uploadpic(){
    if (imgurl.indexOf("http://") < 0) {
        alert(imgurl);
        alert(uploadHttp);
        uexUploaderMgr.createUploader(uopCode, uploadHttp);
    }
    else {
        uexWindow.toast(0, 5, "非本地图片", 1000);
    }
}

window.uexOnload = function(){

    uexWindow.cbActionSheet = function(opId,dataType,data){
        switch(parseInt(data)){
        case 0:
            uexCamera.open();
            break;
        case 1:
            uexFileMgr.explorer("");
            break;
        case 2:
            uexWindow.prompt("提示", "输入网络图片URL", "",["确定","取消"]);
            break;
        }
    }
    uexWindow.cbPrompt=function(opId,dataType,data){
        if(dataType==1){
            var d = eval("("+data+")");
            if(d.num=="0"){
                if(d.value.indexOf('http://')>-1){
                    imgurl=d.value;
                    document.getElementById("imgid").src=imgurl;
                }
                else
                    alert("网络URL错误");
            }
            
        }
    }
    uexCamera.cbOpen = function(opId,dataType,data){
        if(dataType==0)
        {
            imgurl = data;
            document.getElementById("imgid").src=imgurl;
        }
    }
    uexFileMgr.cbExplorer = function(opId,dataType,data){
        uexLog.sendLog(dataType); 
        if(dataType==0){
            uexLog.sendLog(data);
            imgurl = data;
            document.getElementById("imgid").src=imgurl;
            uexLog.sendLog(document.getElementById("imgid").src);
        }
    }
    //****************下载回调***************
    uexDownloaderMgr.cbCreateDownloader=function(opId,dataType,data){
        uexDownloaderMgr.download(dopCode,imgurl,inSavePath,0);
        setLog("开始下载图片");
    }
    uexDownloaderMgr.onStatus = function(opId,fileSize,percent,status){
        if(status == 0)
            setLog("下载进度:"+percent+"%");
        if(status == 1){
            setLog("下载完成");
            imgurl = inSavePath;
            uexDownloaderMgr.closeDownloader(dopCode);
        }
        if(status == 2){
            setLog("下载出错");
            uexDownloaderMgr.closeDownloader(dopCode); 
            uexFileMgr.deleteFileByPath(inSavePath);
        }
    }
    //******************************************
    //****************上传回调***************
    uexUploaderMgr.cbCreateUploader = function(opId,dataType,data){
        uexUploaderMgr.uploadFile(uopCode,imgurl,"filename",1);
        setLog("开始上传图片");
    }
    uexUploaderMgr.onStatus = function(opId,fileSize,percent,serverPath,status){
        if(status==0)
            setLog("上传进度:"+percent+"%");
        if(status == 1){
            setLog("上传完成");
            uexUploaderMgr.closeUploader(uopCode);
        }
        if(status == 2){
            setLog("上传出错");
        }
    }
    //******************************************
}
</script>
  </body>
</html>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace SqsBusiness
{
    /// <summary>
    /// abc1 的摘要说明
    /// </summary>
    public class abc1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");

            try
            {
                string from = context.Request.QueryString["from"];
                string path = context.Server.MapPath("~/") + "upload";
                if (from == "web")
                {
                    context.Response.Write("<script>alert('1')</script>");
                    if (context.Request.Files.Count > 0)
                    {
                        context.Response.Write("<script>alert('2')</script>");

                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }


                        string fileName = Path.GetFileName(context.Request.Files["filename"].FileName);
                        string end = fileName.Substring(fileName.Length - 4).ToLower();


                        if (end.Equals(".jpg") || end.Equals(".png") || end.Equals(".gif") || end.Equals(".mp4"))
                        {
                            context.Request.Files["filename"].SaveAs(path + "\\" + fileName);
                        }
                    }
                }
                if (from == "client")
                {
                    if (context.Request.Files.Count > 0)
                    {
                        if (!Directory.Exists(path))
                            Directory.CreateDirectory(path);
                        string fileName = Path.GetFileName(context.Request.Files["file"].FileName);
                        string end = fileName.Substring(fileName.Length - 4).ToLower();
                        if (end.Equals(".jpg") || end.Equals(".png") || end.Equals(".gif") || end.Equals(".mp4"))
                        {
                            context.Request.Files["file"].SaveAs(path + "\\" + fileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                context.Response.Write("<script>alert('" + ex.Message + "')</script>");
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

posted @ 2012-09-28 16:20  摩根船长  阅读(11196)  评论(2编辑  收藏  举报