我正在尝试调整将通过文件输入上传的图像,然后将其保存到定向文件夹。我尝试了许多可能的方式,但我仍然无法得到它。请帮帮我!
查看
@using (Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" id="btnFileUpload" class="zone" runat="server"/>
}
的Ajax
$('#btnFileUpload').fileupload({
url: '@Url.Action("UploadFile")',
dataType: 'json',
disableImageResize: /Android(?!.*Chrome)|Opera/
.test(window.navigator && navigator.userAgent),
},
success: function (e, data) {
//window.location.reload();
console.log("Done");
console.log(e);
});
控制器
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
string _FileName = "";
string _path = "";
try
{
if (file.ContentLength > 0)
{
_FileName = Path.GetFileName(DateTime.Now.ToBinary() + "-" + file.FileName);
_path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);
file.SaveAs(_path);
imageUrls = "/UploadedFiles/" + _FileName;
}
System.Diagnostics.Debug.WriteLine("File Uploaded Successfully!!");
return Json(imageUrls);
}
catch
{
ViewBag.Message = "File upload failed!!";
System.Diagnostics.Debug.WriteLine("File upload failed!!");
return Json(ViewBag.Message);
}
}
答案 0 :(得分:1)
此处实施您的方案
我认为这会有用
[HttpPost]
public ActionResult UploadFile (HttpPostedFileBase file) {
string _FileName = "";
string _path = "";
try {
if (file.ContentLength > 0) {
_FileName = Path.GetFileName (DateTime.Now.ToBinary () + "-" + file.FileName);
_path = Path.Combine (Server.MapPath ("~/UploadedFiles"), _FileName);
byte[] fileData = null;
using (var binaryReader = new BinaryReader (file.InputStream)) {
fileData = binaryReader.ReadBytes (file.ContentLength);
}
HandleImageUpload (fileData, _path);
imageUrls = "/UploadedFiles/" + _FileName;
}
System.Diagnostics.Debug.WriteLine ("File Uploaded Successfully!!");
return Json (imageUrls);
} catch {
ViewBag.Message = "File upload failed!!";
System.Diagnostics.Debug.WriteLine ("File upload failed!!");
return Json (ViewBag.Message);
}
}
private Image RezizeImage (Image img, int maxWidth, int maxHeight) {
if (img.Height < maxHeight && img.Width < maxWidth) return img;
using (img) {
Double xRatio = (double) img.Width / maxWidth;
Double yRatio = (double) img.Height / maxHeight;
Double ratio = Math.Max (xRatio, yRatio);
int nnx = (int) Math.Floor (img.Width / ratio);
int nny = (int) Math.Floor (img.Height / ratio);
Bitmap cpy = new Bitmap (nnx, nny, PixelFormat.Format32bppArgb);
using (Graphics gr = Graphics.FromImage (cpy)) {
gr.Clear (Color.Transparent);
// This is said to give best quality when resizing images
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.DrawImage (img,
new Rectangle (0, 0, nnx, nny),
new Rectangle (0, 0, img.Width, img.Height),
GraphicsUnit.Pixel);
}
return cpy;
}
}
private MemoryStream BytearrayToStream (byte[] arr) {
return new MemoryStream (arr, 0, arr.Length);
}
private void HandleImageUpload (byte[] binaryImage, string path) {
Image img = RezizeImage (Image.FromStream (BytearrayToStream (binaryImage)), 103, 32);
img.Save (path, System.Drawing.Imaging.ImageFormat.jpg);
}
调整从此thread
获取的代码答案 1 :(得分:0)
此任务有许多优秀的库可以处理您需要的所有内容。
我个人使用ImageProcessor。而且我知道更多的存在具有相同甚至更好的质量。如果你愿意,我可以帮助你使用它,但文档应该足够了。
<强> - 编辑 - 强>
这是使用ImageProcessor的示例代码,看看它是否有帮助:
var imageFactory = new ImageFactory(true);
imageFactory.Load(inputStream).Resize(
new ResizeLayer(new Size(128, 128), ResizeMode.Max)).
Save(path);
另外不要忘记安装所需的nuget包:)