本文整理汇总了C#中IFormFile.CopyToAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IFormFile.CopyToAsync方法的具体用法?C# IFormFile.CopyToAsync怎么用?C# IFormFile.CopyToAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IFormFile
的用法示例。
在下文中一共展示了IFormFile.CopyToAsync方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GuardarImagen
private async Task<bool> GuardarImagen(IFormFile imagen, int personajeId)
{
bool resultado = true;
string carpetaImagenes = Path.Combine(_environment.WebRootPath, "personajes");
string extension = Path.GetExtension(imagen.FileName);
if (!EsExtensionValida(extension))
{
resultado = false;
_mensaje = "La extension de la imagen no es valida. Solo se admiten JPG, JPEG, GIF y PNG";
}
if (resultado && imagen.Length > 1024*1024)
{
resultado = false;
_mensaje = "Te has colado; la imagen no debe ser mayor de 1 mega";
}
if ( resultado && imagen.Length > 0)
{
using (var fileStream = new FileStream(Path.Combine(carpetaImagenes, personajeId.ToString() + ".png"), FileMode.Create))
{
await imagen.CopyToAsync(fileStream);
}
}
return resultado;
}
示例2: Create
public async Task<IActionResult> Create([Bind] Participant partner, IFormFile image)
{
if (this.ModelState.IsValid)
{
var uploads = Path.Combine(this.environment.WebRootPath, "uploads/participants");
if (!Directory.Exists(uploads))
{
Directory.CreateDirectory(uploads);
}
if (image.Length > 0)
{
var fileName = $"{Guid.NewGuid()}.{image.FileName}";
using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create))
{
await image.CopyToAsync(fileStream);
partner.ImgPath = $"/uploads/participants/{fileName}";
}
}
this.context.Add(partner);
await this.context.SaveChangesAsync();
return this.RedirectToAction("Index");
}
return this.View(partner);
}
示例3: UploadDocumentAsync
public async Task UploadDocumentAsync(IFormFile file)
{
using (var fileStream = new FileStream(Path.Combine(_documentUploadConfiguration.UploadPath, file.FileName), FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
}
示例4: ProcessUpload
private async Task<JsonResult> ProcessUpload(IFormFile file)
{
Trace.TraceInformation("Process upload for {0}", file.ToString());
var t = DateTime.UtcNow - new DateTime(1970, 1, 1);
var epoch = (int)t.TotalSeconds;
var sourceName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
var filePath = Path.GetTempFileName();
string url = null;
// Save locally
using (var stream = System.IO.File.OpenWrite(filePath))
{
await file.CopyToAsync(stream);
}
try
{
var resultPaths = ConvertFiles(filePath, sourceName);
if (resultPaths != null && resultPaths.Any())
{
foreach (var path in resultPaths)
{
Trace.TraceInformation("Moving to blob store: {0}", path);
var blobUri = await _storage.UploadFileToBlob(path, Path.GetFileName(path));
// Hack - Grab the destination URI for use later
if (blobUri.Contains(".dat"))
{
url = blobUri.Replace(".dat", string.Empty);
}
}
// update project name if appropriate
string projectName = Path.GetFileNameWithoutExtension(sourceName);
var projects = _storage.GetProjectsForUser(GetEmailFromUser());
if (projects.Where(p => string.Equals(p.Name, projectName, StringComparison.OrdinalIgnoreCase)).Any())
{
int versionNumber = projects.Count(p => p.Name.Contains(projectName)) + 1;
projectName = string.Format("{0} (v{1})", projectName, versionNumber);
}
_storage.SaveFileToTables(projectName, url, GetEmailFromUser());
}
}
catch (Exception ex)
{
var result = new JsonResult(new { error = "The server failed to process this file. Please verify source data is compatible." });
result.StatusCode = 500;
return result;
}
return Json(new { thumbnail = "url" + ".jpg" });
}
示例5: UpdateLogoAsync
public async Task<string> UpdateLogoAsync(string clubName, IFormFile file)
{
string path = clubName;
var relativePath = path;
if (string.IsNullOrEmpty(path) || !path.Contains(LogoPath))
{
var newName = (string.IsNullOrWhiteSpace(path) ? GenerateNewName() : path) + "." + file.FileName.Split('.').Last();
var newPath = GenerateNewPath(LogoPath);
relativePath = Path.Combine(newPath, newName);
path = GetFullPath(relativePath);
}
else
{
path = GetFullPath(path);
}
await file.CopyToAsync(new FileStream(path, FileMode.Create));
relativePath = Regex.Replace(relativePath, "\\\\", "/");
// await _clubService.UpdateLogoAsync(clubName, relativePath);
return relativePath;
}