• c# 上传图片流,php端(laravel框架)接收处理方法


    c# httppost方法

    public struct PostFile
    {
    public string name;
    public string filename;
    public Stream bitmapStream;
    }

    public string PostStringFormPic(Dictionary<string, string> paramData, PostFile postfile, Encoding EncodingPostData = null, Encoding EncodingReadStream = null)
    {
    webRequest.Method = "POST";
    //string ret = string.Empty;
    Stream newStream = null;
    //StreamReader sr = null;

            if (EncodingReadStream == null)
            {
                EncodingReadStream = Encoding.UTF8;
            }
            if (EncodingPostData == null)
            {
                EncodingPostData = Encoding.UTF8;
            }
    
            try
            {
                //EncodingPostData = Encoding.GetEncoding("GB2312");
                // 边界符  
                var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
                // 边界符  
                var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "
    ");
                // 最后的结束符  
                var endBoundary = Encoding.ASCII.GetBytes("
    --" + boundary + "--
    ");
                webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
                var memStream = new MemoryStream();
                memStream.Write(beginBoundary, 0, beginBoundary.Length);
    
                var header = "Content-Disposition: form-data; name="{0}"; filename="{1}"
    " +
         "Content-Type: image/jpeg
    
    ";
                header = string.Format(header, postfile.name, postfile.filename);
                var headerbytes = EncodingPostData.GetBytes(header);
                memStream.Write(headerbytes, 0, headerbytes.Length);
    
    
    
                postfile.bitmapStream.CopyTo(memStream);
    
    
    
                var stringKeyHeader = "
    --" + boundary +
                          "
    Content-Disposition: form-data; name="{0}"" +
                          "
    
    {1}";
    
    
                // var rn = "
    ";//换行
                // var rnlength = rn.Length;
                foreach (var item in paramData)
                {
    
    
                    string data = string.Format(stringKeyHeader, item.Key, item.Value);
                    byte[] bdata = EncodingPostData.GetBytes(data);
                    memStream.Write(bdata, 0, bdata.Length);
                    //memStream.Write(EncodingPostData.GetBytes(rn), 0, rnlength);
    
                }
    
    
                // 写入最后的结束边界符  
                memStream.Write(endBoundary, 0, endBoundary.Length);
    
                webRequest.ContentLength = memStream.Length;
                //byte[] byteArray = EncodingPostData.GetBytes(paramData); //转化
                // byte[] byteArray = Encoding.ASCII.GetBytes(paramData);
                // webRequest.ContentLength = byteArray.Length;
                ServicePointManager.Expect100Continue = false;
    
                newStream = webRequest.GetRequestStream();
    
                memStream.Position = 0;
                var tempBuffer = new byte[memStream.Length];
                memStream.Read(tempBuffer, 0, tempBuffer.Length);
                memStream.Close();
    
                newStream.Write(tempBuffer, 0, tempBuffer.Length);//写入参数
                newStream.Close();
                //webResponse = (HttpWebResponse)webRequest.GetResponse();
                ////CheckProxy();
                //sr = new StreamReader(webResponse.GetResponseStream(), EncodingReadStream);
                //ret = sr.ReadToEnd();
                //return ret;
    
                string result = string.Empty;
                webResponse = (HttpWebResponse)webRequest.GetResponse();
                CheckProxy();
    
                if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压
                {
                    using (System.IO.Stream stream = webResponse.GetResponseStream())
                    {
                        using (var zipStream =
                            new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress))
                        {
                            using (System.IO.StreamReader sr = new System.IO.StreamReader(zipStream, EncodingReadStream))
                            {
                                result = sr.ReadToEnd();
                            }
                        }
                    }
                }
                else
                {
                    using (System.IO.Stream stream = webResponse.GetResponseStream())
                    {
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(stream, EncodingReadStream))
                        {
                            result = sr.ReadToEnd();
                        }
                    }
                }
    
                return result;
            }
            finally
            {
    
                if (WebResponse != null)
                    WebResponse.Close();
                if (newStream != null)
                    newStream.Close();
            }
        }
    

    php端接收 laravel框架

    public function upload_headimg(Request $request)
    {
    //解析回传数据
    $extend_data = json_decode(urldecode($request->input('extend_data', '')));

        try {
    
            $wxnum_model = new ModelsWxnum();
            //微信公众号信息
            $wxnum_info = $wxnum_model->select_wxnuminfo($extend_data->task_id);
    
            $pb_headimg = array();
            if ($request->hasFile('fileName')) {
                $pb_headimg = $wxnum_model->update_headimg($wxnum_info, $request->file()['fileName']->getRealPath());
            }
    
            if (count($pb_headimg) > 0) ModelsWxnum::where('id', $wxnum_info->id)->update($pb_headimg);
    
            return ModelsResponseRet::return_msg_success();
        } catch (Exception $e) {
            Log::info('上传公众号头像:', $request->all());
            Log::error(sprintf("%s 上传公众号头像失败 %s", $request->input('u_name', ''), $e->getMessage()));
            return ModelsResponseRet::return_msg('1013');
        }
    }
  • 相关阅读:
    RabbitMQ安装(发生系统错误5。拒绝访问。发生系统错误1067。进程意外终止。)
    SQLServer执行脚本提示“系统找不到指定的文件”或“内存资源不足”
    TypeScript@HelloWorld!
    超详细Node安装教程
    进制转换
    菜鸟成长记
    ASP.NET Core中使用MialKit实现邮件发送
    VS未能正确加载 ”Microsoft.VisualStudio.Editor.Implementation.EditorPackate“包错误解决方法
    C#Winfrom Listview数据导入Excel
    安装研发服务器
  • 原文地址:https://www.cnblogs.com/bangejingting/p/6377489.html
Copyright © 2020-2023  润新知