ارور 500
سلام استاد من یه پروژ نوشتم الان روی هاست چندماهی هست کار میکنه الان جدیدا توی آپلود عکس ارور 500سرور میده توی همه ی قسمت های سایتم اینجوریه تو سیستم خود و دیباگ میگیرم هیچ مشکلی نیست روی سرور اینجوریه پشتیبانی میگه سرور ما مشکلی نداره الان چجوری مشکل رو پیدا کنیم ؟؟رو سیستمم همه چی اوکی هست
پاسخ ها
فرهاد اصلانی
سلام توی کلاس program این کد رو بنویس و آپلود کن ببین ارور رو بهت نشون میده
app.UseDeveloperExceptionPage()
فرهاد اصلانی
البته پابلیش کنید حتما تا روی سایت بهتون ارور رو نشون بده
Saeed Papi
An unhandled exception occurred while processing the request.
UnauthorizedAccessException: Access to the path 'E:\vhosts\draryasadr.com\httpdocs\wwwroot\img\userAvatar\orgin\7641af70fd6d4f80b9dfd93f49c358fe.jpg' is denied.
Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(string fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
InvalidOperationException: The exception handler configured on ExceptionHandlerOptions produced a 404 status response. This InvalidOperationException containing the original exception was thrown since this is often due to a misconfigured ExceptionHandlingPath. If the exception handler is expected to return 404 status responses then set AllowStatusCode404Response to true.
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddlewareImpl.HandleException(HttpContext context, ExceptionDispatchInfo edi)
Saeed Papi
public static string UserAvatarOrgin = "/img/userAvatar/orgin/";
public static string UserAvatarOrginServer = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img/userAvatar/orgin/");
public static string UserAvatarThumb = "/img/userAvatar/thumb/";
public static string UserAvatarThumbServer = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img/userAvatar/thumb/");
فرهاد اصلانی
این داره ارور authorize میده . به احتمال ۹۰ درصد مشکل از تنظیمات پلسک هست . ولی شما کد زیر رو تست کنید ببینید چه اتفاقی میفته
فرهاد اصلانی
try { File.Delete(path); } catch (UnauthorizedAccessException) { FileAttributes attributes = File.GetAttributes(path); if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { attributes &= ~FileAttributes.ReadOnly; File.SetAttributes(path, attributes); File.Delete(path); } else { throw; } }
Saeed Papi
میشه یه توضیح بدید دقیقا این کد را کجا استفاده کنم
فرهاد اصلانی
اون قسمت ار کدتون که با فایل کار میکنید و ارور داره رو برام بفرستید تا براتون بگم
Saeed Papi
if (userAvatar != null && userAvatar.IsImage())
{
var imageName = Guid.NewGuid().ToString("N") + Path.GetExtension(userAvatar.FileName);
userAvatar.AddImageToServer(imageName, PathExtensions.UserAvatarOrginServer, 100, 100, PathExtensions.UserAvatarThumbServer,user.Avatar);
user.Avatar = imageName;
}
این کد توی سرویس هست
این کد توی PathExtensions:
public static string UserAvatarOrgin = "/img/userAvatar/orgin/";
public static string UserAvatarOrginServer =Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img/userAvatar/orgin/");
public static string UserAvatarThumb = "/img/userAvatar/thumb/";
public static string UserAvatarThumbServer = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img/userAvatar/thumb/");
Saeed Papi
public static bool AddImageToServer(this IFormFile image, string fileName, string orginalPath, int? width, int? height, string thumbPath = null, string deletefileName = null)
{
if (image != null && image.IsImage())
{
if (!Directory.Exists(orginalPath))
Directory.CreateDirectory(orginalPath);
if (!string.IsNullOrEmpty(deletefileName))
{
if (File.Exists(orginalPath + deletefileName))
File.Delete(orginalPath + deletefileName);
if (!string.IsNullOrEmpty(thumbPath))
{
if (File.Exists(thumbPath + deletefileName))
File.Delete(thumbPath + deletefileName);
}
}
string OriginPath = orginalPath + fileName;
using (var stream = new FileStream(OriginPath, FileMode.Create))
{
if (!Directory.Exists(OriginPath)) image.CopyTo(stream);
}
if (!string.IsNullOrEmpty(thumbPath))
{
if (!Directory.Exists(thumbPath))
Directory.CreateDirectory(thumbPath);
ImageOptimizer resizer = new ImageOptimizer();
if (width != null && height != null)
resizer.ImageResizer(orginalPath + fileName, thumbPath + fileName, width, height);
}
return true;
}
return false;
Saeed Papi
آدرس URL اصلی تصویر یافت نشد.
وقتی باCkeditor میخوام عکس آپلود کنم این ارور رو میده
فرهاد اصلانی
من توی دوزه از extention mrthod استفاده نکردما ؟
ولی در کل این ارور به احتمال زیاد برای readOnly بودن هست زمانی که میخواید عکس رو پاک کنید .
try
{
File.Delete(path);
}
catch (UnauthorizedAccessException)
{
FileAttributes attributes = File.GetAttributes(path);
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
attributes &= ~FileAttributes.ReadOnly;
File.SetAttributes(path, attributes);
File.Delete(path);
}
else
{
throw;
}
}
اگه مشکل زا readonly بودن باشه کد بالا اون رو رفع میکنه .
ولی در کل اگر دقیقا مثل دوره جلو برید مشکلی پیش نمیاد .
در واقع نیازی به extentiom method نیست
public interface IFileService
{
string UploadImage(IFormFile file, string folder);
bool DeleteImage(string route);
bool ResizeImage(string imageName, string Folder, int newSize);
}
اینترفیس بالا و
پیاده سازیش هم میشه این
internal class FileService : IFileService
{
private readonly IWebHostEnvironment _environment;
public FileService(IWebHostEnvironment environment)
{
_environment = environment;
}
public bool DeleteImage(string route)
{
var directory = $"{_environment.WebRootPath}//{route}";
if (File.Exists(directory))
{
try
{
File.Delete(directory);
return true;
}
catch
{
return false;
}
}
return false;
}
public bool ResizeImage(string imageName, string Folder, int newSize)
{
var directory = $"{_environment.WebRootPath}/Images/{Folder}/{imageName}";
var newDirectory = $"{_environment.WebRootPath}/Images/{Folder}/{newSize}";
if (!Directory.Exists(newDirectory))
Directory.CreateDirectory(newDirectory);
try
{
using var image = Image.Load(directory);
image.Mutate(x => x.Resize(newSize, 0));
image.Save(newDirectory + "/" + imageName);
return true;
}
catch (Exception)
{
return false;
}
}
public string UploadImage(IFormFile file, string folder)
{
if (file == null) return "";
var directory = $"{_environment.WebRootPath}//Images//{folder}";
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
var fileName = $"{Guid.NewGuid()}{file.FileName}";
var path = $"{directory}//{fileName}";
using (var output = File.Create(path))
{
try
{
file.CopyTo(output);
return fileName;
}
catch
{
return "";
}
}
}
}
Saeed Papi
درسته این کد رو از پروژهای قبلیم زدم و کار میکرده تو هاست تا چندروز پیش و روی لوکال هاستمم کار میکنه بدون مشکل
مشکل اصلی اینه عکس که میخواد بیاد تو هاست یجورایی null میشه
برنامه هم عکس رو پیدا نمیکنه که آپلود کنه
پشتیبانی سرور هم میگه بگید چیو براتون فعال کنم تا براتون فعال کنم سطح دسترسی ها هم میگه همش فعاله
Saeed Papi
استاد درسترش کردم توی Directory permissions دسترسی نداشت به wwwroot
مرسی از وقتی که برام گذاشتید
فرهاد اصلانی
خواهش میکنم