zoukankan      html  css  js  c++  java
  • C#使用HTML文件中的file文件上传,用C#代码接收上传文件

    单独做图片上传很简单,如果要客户端要上传头像保存到服务器就要稍微麻烦一点点了。

    不多说了,直接上源码:

    private void Upload()        

    {

                string jsonInfo = string.Empty;

      

        ///这句是关键,它是获取HTTP中文件流 的对象集合。

                HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;

                string mobile = string.IsNullOrEmpty(Request.Form["mobile"]) ? Request.QueryString["mobile"] : Request.Form["mobile"];

                string fName = "";

                // string.IsNullOrEmpty(Request.Form["filename"]) ? Request.QueryString["filename"] : Request.Form["filename"];

                string status = string.Empty;

                string error = string.Empty;

        ///在这里我们只取其中一个上传对象操作

                 fName = hfc[0].FileName;

                try

                {

                    string[] list;

                    if (!string.IsNullOrEmpty(fName))

                    {

                        list = fName.Split('.');

                        if (list.Length > 1)

                        {

                            string ftype = list[1].ToLower();

                            if (ftype == "jpg" || ftype == "png" || ftype == "jpeg")

                            {

                                fName = mobile + "." + list[1];

                                string filePath = "../CSS/headmiages/";

                                hfc[0].SaveAs(System.IO.Path.Combine(MapPath(filePath), fName));

                                RockUserInfo userInfo = new RockUserInfo();

                                userInfo.LogOnPhoneNum = mobile;

                                //根据手机号查询出用户基本信息

                                userInfo = SearchByPhone(userInfo);

                                userInfo.filename = fName;

                                //插入头像信息保存

                                Save(userInfo);

                                status = "0";

                                error = "";

                            }

                            else

                            {

                                status = "1";

                                error = "请上传 .jpg/.png/.jpeg/类型的图片";

                            }

                        }

                    }

                    else

                    {

                        status = "1";

                        error = "请上传 .jpg/.png/.jpeg/类型的图片";

                    }

                    //fileload.in

                }

                catch (Exception ex)

                {

                    status = "2";

                    error = ex.ToString();

                }

                jsonInfo = "{"" + "status" + "":"" + status + ""," + "msg" + "":"" + error + "}";

                Response.Write(jsonInfo);

            }

    这样就 OK了,在服务器端保存上传文件,操作数据库就基本达到目标了。很简单吧!

    这段代码经过测试是没有问题的,如果有不合理之处还望大家指出来共同进步。

  • 相关阅读:
    load custom class in drupal 8
    HEAD in Git
    composer version constraint 版本号前缀
    如何测试
    看待一段数据
    创建一个plugin
    eclipse的快捷方式
    .git文件夹的猜想
    本地可以但远程不行
    方法点不进去的原因
  • 原文地址:https://www.cnblogs.com/miao817/p/3607452.html
Copyright © 2011-2022 走看看