PDA

View Full Version : آپلود تصویر



davidrobert
جمعه 21 دی 1397, 00:26 صبح
سلام و خسته نباشید.
دوستان از کی سعی بر آپلود یه تصویر تو مسیر جاری دارم ولی متاسفانه همش مسیر رو میبره به درایو C که ربطی به مسیر جاری بنده نداره. و امکان آپلود تصویر رو ندارم.
در Page

public async Task<IActionResult> OnPostAsync()
{
if (ModelState.IsValid)
{
if (Image != null)
{

var fileContent = ContentDispositionHeaderValue.Parse(Image.ContentD isposition);

// Some browsers send file names with full path.
// We are only interested in the file name.
var fileName = GetUniqueName(Path.GetFileName(fileContent.FileNam e.ToString().Trim('"')));

//The below line gives the error
//var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, "App_Data", fileName);

//and I replace with this (but we can use this variable at multiple location by assign a physical path)
// var physicalPath = Path.Combine(Path.Combine(Directory.GetCurrentDire ctory(), "wwwroot"), "Images", fileName);
var physicalPath = $"{Directory.GetCurrentDirectory()}{@"\wwwroot\images"+ fileName}";

//The files are not actually saved in this demo
using (var fileStream = new FileStream(physicalPath, FileMode.Create))
{
await Image.CopyToAsync(fileStream);
}
string link = physicalPath;
Phone.Photo = link.ToString();


phones.Insert(this.Phone.Name, this.Phone.Last, this.Phone.Photo, this.Phone.Email, this.Phone.Mobile);
}


}
return Page();
}


تصویر خطا
149576
دوستان اگه فایلی برای آپلود تصویر فقط خواهشا با PageMode Asp Core باشه نه Controller روی اینترنت هرچی میگردم 90 درصد Controller میاره ولی من Controller نمیخوام کار کنم Page میخوام کار کنم دستور آپلود تصویر داشته باشید فایلش رو ممنون میشم.

مهدی کرامتی
جمعه 21 دی 1397, 11:39 صبح
سلام
خطی که آدرس محل ذخیره فایل را محاسبه می کند میبایست به این شکل باشد:
var physicalPath = Path.Combine(Server.MapPath("~/","Images"));

davidrobert
دوشنبه 24 دی 1397, 11:21 صبح
سلام و خسته نباشبد در آپلود تصویر با این خطا مواجه شدم که میگه دسترسی مقدور نیست.
149602
و از این دستورات استفاده میکنم.

private IHostingEnvironment _hostingEnvironment;
public IndexModel(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public async Task OnPostAsync(IFormFile image)
{
// var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot/uploads", image.FileName);
string folderName = "Upload";
string webRootPath = _hostingEnvironment.WebRootPath;
string newPath = Path.Combine(webRootPath, folderName);
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
var memory = new MemoryStream();
using (var stream = new FileStream(newPath, FileMode.Create))
{
await image.CopyToAsync(stream);
}

جستجو هم کردم و این کارها رو داخل Startup.cs
انجام دادم. ولی جوابی نگرفتم.

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// services.Configure<CookiePolicyOptions>(options =>
// {
// // This lambda determines whether user consent for non-essential cookies is needed for a given request.
// options.CheckConsentNeeded = context => true;
// options.MinimumSameSitePolicy = SameSiteMode.None;
// });


// services.AddMvc().SetCompatibilityVersion(Compatib ilityVersion.Version_2_2);
services.AddMvc();
services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(ILoggerFactory loggerFactory,IApplicationBuilder app, IHostingEnvironment env)
{
//if (env.IsDevelopment())
//{
// app.UseDeveloperExceptionPage();
//}
//else
//{
// app.UseExceptionHandler("/Error");
// // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
// app.UseHsts();
//}

//app.UseHttpsRedirection();
//app.UseStaticFiles();
//app.UseCookiePolicy();
app.UseStaticFiles();
//app.UseMvc();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
//app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Error");
}

app.UseStaticFiles();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});

مهدی کرامتی
دوشنبه 24 دی 1397, 11:31 صبح
سلام.



در File Explorer روی فولدر آپلود تان رایت کلیک کنید و Properties را انتخاب کنید.
در تب Security کاربر Network Service را در لیست کاربران انتخاب کنید و دسترسی Full به آن بدهید.

davidrobert
دوشنبه 24 دی 1397, 12:30 عصر
سلام.



در File Explorer روی فولدر آپلود تان رایت کلیک کنید و Properties را انتخاب کنید.
در تب Security کاربر Network Service را در لیست کاربران انتخاب کنید و دسترسی Full به آن بدهید.



سلام ممنون بابت کمکتان دسترسی نداد الان چه کاری باید انجام بیدم این هم دستورات بنده.

public void OnGet()
{

}
private IHostingEnvironment _hostingEnvironment;
public IndexModel(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public async Task OnPostAsync(IFormFile image)
{
// var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot/uploads", image.FileName);
string folderName = "Upload";
string webRootPath = _hostingEnvironment.WebRootPath;
string newPath = Path.Combine(webRootPath, folderName);
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
var memory = new MemoryStream();
using (var stream = new FileStream(newPath, FileMode.Create))
{
await image.CopyToAsync(stream);
}
}

مهدی کرامتی
دوشنبه 24 دی 1397, 12:41 عصر
اشکال کار اینجا بود که شما دارید تو کدتون سعی می کنید فایل رو بعنوان یک دایرکتوری ذخیره کنید!
کد صحیح اش این طوری میشه:
public async Task OnPostAsync(IFormFile image)
{
// var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot/uploads", image.FileName);
string folderName = "Upload";
string webRootPath = _hostingEnvironment.WebRootPath;
string newPath = Path.Combine(webRootPath, folderName);
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
var filename = Path.Combile(newPath,Path.GetFilename(image.Filena me));
var memory = new MemoryStream();
using (var stream = new FileStream(filename, FileMode.Create))
{
await image.CopyToAsync(stream);
}
}