[ServiceStack.Route("/images/webuploader")]
public class WebUploaderImage : IReturn<string>
{
public string id { get; set; }
public string name { get; set; }
public string type { get; set; }
public string lastModifiedDate { get; set; }
public int size { get; set; }
//public int referid { get; set; }
public Guid? referimgid { get; set; }
}
[Authenticate]
public class UploadImageService : AppService
{
public async Task<string> Post(WebUploaderImage request)
{
if (request.referimgid == default)
throw new Exception("not found ");
//logger.Info(request.SerializeToString());
var newImageName = Guid.NewGuid().ToString("N").Substring(0, 20) + ".jpg";
var folderName = DateTime.Now.ToString("yyyyMMdd");
var savedDirectory = f.WebRootPath.CombineWith($"/wx_images/{folderName}/");
var thumbDirectory = f.WebRootPath.CombineWith($"/wx_images/thumbnail/{folderName}/");
savedDirectory.AssertDir();
thumbDirectory.AssertDir();
var savedFilePath = savedDirectory.CombineWith(newImageName);
var thumbFilePath = thumbDirectory.CombineWith(newImageName);
string imageFileName;
if (ImageFileHelper.IsImage(request.name))
{
var bytes = Request.InputStream.ToBytes();
var uploadImage = Image.FromStream(new MemoryStream(bytes));
var savedImage = uploadImage;
var thumbImage = uploadImage.Scale((double)240 / uploadImage.Width);
savedImage.Save(savedFilePath, ImageFormat.Jpeg);
thumbImage.Save(thumbFilePath, ImageFormat.Jpeg);
imageFileName = $"/{folderName}/{newImageName}";
}
else
{
var bytes = Request.InputStream.ToBytes();
var fileExtension = Path.GetExtension(request.name.ToLower());
if (fileExtension.IsNullOrWhiteSpace())
{
fileExtension = ".jpg";
}
var allowedFileExtensions = new string[] { ".jpg", ".jpeg", ".png", ".bmp", ".rar", ".ppt", ".pptx", ".doc", ".docx", ".xls", ".xlsx", ".pdf", ".txt", ".mp4" };
if (allowedFileExtensions.Contains(fileExtension) == false)
{
logger.Error($"not allowed, :{ fileExtension}, request.name: {request.name}");
throw new Exception($"not allowed, :{ fileExtension}, request.name: {request.name}");
}
var newFileName = $"{Guid.NewGuid().ToString("N").Substring(0, 20) + fileExtension}";
savedFilePath = savedDirectory.CombineWith(newFileName);
try
{
using var ms = new MemoryStream(bytes);
using var fs = new FileStream(savedFilePath, FileMode.OpenOrCreate);
ms.WriteTo(fs);
ms.Close();
fs.Close();
}
catch (Exception ex)
{
logger.Error("upload error:" + request.SerializeToString() + ex.Message);
}
imageFileName = $"/{folderName}/{newFileName}";
}
return imageFileName;
}
}
Used PC to upload Image, works fine. when used iphone Upload Image, some times is ok , but some times get error, below image is the logfile when get error. after 2020-11-28 07:26:57.1015 【DEBUG】 【HttpAsyncTaskHandler】 CreateRequestAsync/requestParams:referimgid,id,name,type,lastModifiedDate,size has lot of unrecognizable code, why will receive lots of these? how to reject it or not receive it ? thanks.