fireMonkey上传图片至asp.net Core服务器

//delphi客户端源码
uses
  IdMultipartFormData;


procedure TForm1.Button1Click(Sender: TObject);  //上传
var
  ABitmapCodecSaveParams:TBitmapCodecSaveParams;
  ASuperObject:ISuperObject;
MS: TStringStream;
postStream : TIdMultiPartFormDataStream;
begin
  FFileName:=Self.edtFileName.Text;
  FFileDir:=Self.edtUploadType.Text;
  FServerUrl:='http://'+Self.edtServer.Text+':'+Self.edtPort.Text;
  //压缩比50%
  ABitmapCodecSaveParams.Quality:=70;
  //保存成文件
  Self.imgUploadPicture.Prop.Picture.SaveToFile(
                    GetApplicationPath+FFileName,
                    @ABitmapCodecSaveParams
                    );
  try
    IdHTTP1.HandleRedirects := true;


    postStream := TIdMultiPartFormDataStream.Create;
    postStream.AddFormField('param','上传图片测试','UTF-8').ContentTransfer := '8bit' ; // 表单参数
    postStream.AddFile('file', GetApplicationPath+FFileName, 'image/png'); // 图像文件
    Memo1.Text := Utf8ToAnsi(IdHTTP1.Post('http://localhost:59223/api/upload', postStream));
    ASuperObject:=TSuperObject.Create(Memo1.Text);
    if ASuperObject.I['statusCode']=200 then
       begin
            ShowMessage('上传成功');
       end
       else
        showmessage('上传失败');
  finally
    postStream.Free;
  end;
end;




///asp.net core 是一个比较优秀的web服务端技术,可以发布在win  linux   mac系统上面
///


//asp.net core服务端  webapi控制器   


  [Route("api/[controller]")]
    public class UpLoadController : Controller
    {
        private readonly Chloe.IDbContext _dbContext;
        private IHostingEnvironment _environment;
        private IConfiguration _configuration;
        public UpLoadController(Chloe.IDbContext dbContext, IHostingEnvironment environment, IConfiguration Configuration)
        {
            _dbContext = dbContext;
            _environment = environment;
            _configuration = Configuration;
        }


        [HttpPost]
        public async Task<HttpResponseMessage> Upload()
        {
            string _filename = "";
            string type = "";
            string _path = "images/app";
            var keys = Request.Form.Keys.ToString();
            var files = Request.Form.Files;
            //保存上传的图片
            var uploads = Path.Combine(_environment.WebRootPath, _path);
            //返回图片地址


            string _s =HttpContext.Request.Form["param"].ToString();


            string filePhysicalPath = uploads;
            if (!Directory.Exists(filePhysicalPath))//判断上传文件夹是否存在,若不存在,则创建
            {
                Directory.CreateDirectory(filePhysicalPath);//创建文件夹
            }
            try
            {
                foreach (var file in files)
                {
                    if (file.Length > 0)
                    {
                        type = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);
                        _filename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + "." + type;
                        using (var fileStream = new FileStream(Path.Combine(uploads, _filename), FileMode.Create))
                        {
                           //使用异步操作,保存文件 
                           await  file.CopyToAsync(fileStream);
                           ///插入数据库中操作 
                           
                           ///
                            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                            return response;
                        }
                    }
                }
            }
            catch
            {
            }
            HttpResponseMessage response1 = new HttpResponseMessage(HttpStatusCode.InternalServerError);
            return response1;
        }


    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值