//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;
}
}
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;
}
}