android 上传图片

时间:2020-01-14
本文章向大家介绍android 上传图片,主要包括android 上传图片使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.先用html的原始标签来完成上传图片功能,后台使用asp.net.

2.再用android的okhttp来代替html。

html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>HTML控件配合ASP。NET 上传</title>
</head>
<body>
<form name="uploadForm" method="post" enctype="multipart/form-data" action="http://localhost/upload/Default.aspx">
    <input id="name" name="name" style="width:220px;" />
    <input type="file" id="imgFile" name="imgFile" style="width:220px;" />
    <input type="file" id="imgFile2" name="imgFile2" style="width:220px;" />
    <input type="file" id="imgFile3" name="imgFile3" style="width:220px;" />
    <input type="submit" value="submit" />
</form>
</body>
</html>

asp.net 的后端

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="upload._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace upload
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                string name=this.Request.Form.Get("name");
                string path = Server.MapPath("");
                this.Label1.Text = "name:" + name + ".path:" + path;

                if (this.Request.Files.Count > 0)
                {
                    for (int i = 0; i < this.Request.Files.Count;i++ )
                    {
                        HttpPostedFile file = this.Request.Files.Get(i);
                        string pathstr = path + "/aaupload"+System.DateTime.Now.ToFileTime().ToString() +"index"+  i.ToString()+ ".png";
                        try
                        {
                            file.SaveAs(pathstr);
                        }
                        catch
                        {
                            this.Label1.Text ="error:"+pathstr;
                        }
                    }
                }
            }
        }
    }
}

原文地址:https://www.cnblogs.com/lsfv/p/12193171.html