我使用c#web api为图像上传编写了代码。现在我想在服务器上完成图像上传。这件事也完成了。但我想在服务器上传之后压缩图像。但我怎么能不知道任何人知道怎么办呢,请告诉我。我想压缩图像,然后使用webapi上传到amzon s3服务器上。
这是我的代码=>
[HttpPost]
[Route("FileUpload")]
public HttpResponseMessage FileUpload()
{
try
{
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
foreach (string file in httpRequest.Files)
{
var postedFile = httpRequest.Files[file];
string fname = System.IO.Path.GetFileNameWithoutExtension(postedFile.FileName.ToString());
string extension = Path.GetExtension(postedFile.FileName);
Image img = null;
string newFileName = "";
string path = "";
img = Image.FromStream(postedFile.InputStream);
string path = ConfigurationManager.AppSettings["ImageUploadPath"].ToString();
newFileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".jpeg";
string filePath = Path.Combine(path, newFileName);
UploadImageOnServer(postedFile.InputStream,path + newFileName);
}
}
}
catch (Exception ex)
{
}
return Request.CreateResponse(HttpStatusCode.OK, "Done");
}
这是我的上传代码服务器=>
public static void UploadImageOnServer(Stream File, string Key)
{
var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1);
PutObjectRequest putRequest = new PutObjectRequest
{
BucketName = Bucketname,
InputStream = File,
CannedACL = S3CannedACL.PublicRead,
Key = Key
};
PutObjectResponse response = client.PutObject(putRequest);
}
这是我现在的代码,我想压缩图像。请任何人知道,请告诉我。