手机端调用相机上传图片

表单 布局:


 <form method="post" enctype="multipart/form-data" id="form_UpFile" name="form_UpFile">
<input type="file" accept="image/*" capture="camera" id="file_upfiles" class="external" style="width:170px;" />

<button type="button" id="btn_upload" class="external">上传</button>
<input type="text" value="" id="picPath"  style="display:none;" />
 </form>    

//上传图片或者文件方法
function UploadOther() {
$(“#btn_upload”).on(“click”, function (e) {
alert(“s”);

    var fd = new FormData();

    fd.append("file_upfiles", $("#file_upfiles").get(0).files[0]);
    fd.append("gid", userId);
    console.info(fd);
    $.ajax({
        url: "ajaxpage/GetHelper.ashx",
        type: "POST",
        data: fd,
        cache: false,
        processData: false,
        contentType: false,
        success: function (d) {

            var josn = eval("(" + d + ")");
                   console.info(josn);

                   console.info(josn.filename);

                   $$("#form1 #picPath").val(josn.fileid);
                       $$("#form1 #hs_pic").val(josn.fileid);
                   path = josn.repath;

            console.info(d);
        }
    });
});

}

后台方法:
///
/// 上传文件
///
///
private string fileUpLoad(HttpContext context)
{var userid=context.Request[“gid”];
string uploadPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + “Upload/”;
string result = string.Empty;
string imgPath = string.Empty;
string temp_Guid = Guid.NewGuid().ToString();
string province = “”;
string fName = “”;
StringBuilder gtreturn = new StringBuilder();
StringBuilder sql = new StringBuilder();
float MyFileSize= 0;
HttpRequest request = System.Web.HttpContext.Current.Request;
HttpFileCollection fileCollection = request.Files;
string img_type = “”;

    // 判断是否有文件
    if (fileCollection.Count > 0)
    {

            // 获取文件
        HttpPostedFile httpPostedFile = fileCollection[0];
        // 文件扩展名
        string fileExtension = System.IO.Path.GetExtension(httpPostedFile.FileName);
        var g_type=fileExtension.Split('.');
        img_type = "image/" + g_type[1]; //文件类型  
        string fileName = System.IO.Path.GetFileName(httpPostedFile.FileName);
        fName = fileName;

        MyFileSize = (float)httpPostedFile.ContentLength ;//获取文件的大小
        /// (1024 * 1024)
        // 文件名称
        fileName = Guid.NewGuid().ToString() + "_" + fileName;
        // 文件上传路径
        string filePath = uploadPath + fileName;
        // 验证文件格式

            // 如果目录不存在则要先创建
            if (!System.IO.Directory.Exists(uploadPath))
            {
                System.IO.Directory.CreateDirectory(uploadPath);
            }
            // 保存新的文件
            while (System.IO.File.Exists(filePath))
            {
                fileName = temp_Guid + fileExtension;
                filePath = uploadPath + fileName;
            }
            httpPostedFile.SaveAs(filePath);
            result = filePath;




        sql.AppendFormat("    INSERT INTO  library_file VALUES ( ");
        sql.AppendFormat(" 'hailing','{0}','','{1}','{2}','{3}',",temp_Guid,temp_Guid,fileName,img_type);
        sql.AppendFormat(" '{0}','{1}','{2}','{3}','{4}','{5}',", MyFileSize, fileExtension,"","image",null,"ACTIVE");
        sql.AppendFormat(" '{0}','{1}','{2}','{3}','{4}','{5}','{6}',", DateTime.Now.ToString(), null,null,null,null,null,null);
        sql.AppendFormat(" '{0}','{1}','{2}','{3}','{4}'", userid, null, null, null, null);
        sql.AppendFormat(" )");

        gtreturn.Append("{");


        OperationDB odb = new OperationDB();
        odb.setTableData(sql.ToString());
        gtreturn.Append("\"filename\":\"" + fileName + "\",");
        gtreturn.Append("\"fileid\":\"" + temp_Guid + "\",");


        gtreturn.Append("\"repath\":\"../Upload/" + fileName + "\"");

    }
    else
    {
        result = "请先选择文件!";
        gtreturn.Append("\"absoultPath\":\"" + result + "\",");

    }
    gtreturn.Append("}");
    return gtreturn.ToString();
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值