• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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 + "\r\n");
            // 最后的结束符  
            var endBoundary = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            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}\"\r\n" +
     "Content-Type: image/jpeg\r\n\r\n";
            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 = "\r\n--" + boundary +
                      "\r\nContent-Disposition: form-data; name=\"{0}\"" +
                      "\r\n\r\n{1}";


            // var rn = "\r\n";//换行
            // 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 Models\Wxnum();
        //微信公众号信息
        $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) Models\Wxnum::where('id', $wxnum_info->id)->update($pb_headimg);

        return Models\ResponseRet::return_msg_success();
    } catch (\Exception $e) {
        Log::info('上传公众号头像:', $request->all());
        Log::error(sprintf("%s 上传公众号头像失败 %s", $request->input('u_name', ''), $e->getMessage()));
        return Models\ResponseRet::return_msg('1013');
    }
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP功能函数---替换标签字符发布时间:2022-07-10
下一篇:
PHP学习(8)——面向对象(下)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap