public string SaveImageAzureBlob(string files) { try { var st = JsonConvert.DeserializeObject <IFormFile>(files); byte[] bytes = Convert.FromBase64String(files); BlobContainerClient blop = DadosStorage.OperacaoDeLigaçãoExistente(); string url = null; var stream = new MemoryStream(bytes); IFormFile file = new FormFile(stream, 0, bytes.Length, "filename", "file.jpg"); var localFileName = Path.GetFileName(file.FileName); MemoryStream ms = new MemoryStream(); file.CopyTo(ms); BlobClient blobClient = blop.GetBlobClient(localFileName); ms.Position = 0; blobClient.Upload(ms, true); url = blobClient.Uri.OriginalString; return(url); } catch (Exception msg) { throw new Exception(msg.Message); } }
public async Task WhenISendThePDFToTheAPI() { var fileStream = System.IO.File.OpenRead(@_scenarioContext["FilePath"].ToString()); var formFile = new FormFile(fileStream, 0, fileStream.Length, "FileToUpload", _scenarioContext["FileName"].ToString()); var memStream = new MemoryStream(); formFile.CopyTo(memStream); HttpContent httpContent = new StreamContent(memStream); httpContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = "FileToUpload", FileName = _scenarioContext["FileName"].ToString() }; httpContent.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/pdf"); var formData = new MultipartFormDataContent(); formData.Add(httpContent); var httpClient = (HttpClient)_scenarioContext["HTTPClient"]; var response = await httpClient.PostAsync("api/documents", formData); _scenarioContext.Add("HTTPResponse", response); }
public string Upload(FormFile file, string index) { var fileName = file.FileName; var sPath = basePath + "\\images\\asn-check"; if (!Directory.Exists(sPath)) { Directory.CreateDirectory(sPath); } var i = fileName.LastIndexOf("."); var ext = fileName.Substring(i); var shortName = index + "-" + fileName; var sFileName = sPath + "\\" + shortName; var vName = "images/asn-check/" + shortName; FileInfo f = new FileInfo(sFileName); if (!f.Exists) { using (FileStream fs = new FileStream(file.ToString(), FileMode.Create)) { file.CopyTo(fs); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(fs);//将MemoryStream对象转换成Bitmap对象 bmp.Save(sFileName); } } return(vName); }
private string UploadedFileElsewhere(Listing model) //come back to this (Probably a problem in the view) not allowing me to click on the upload file form { string uniqueFileName = null; var stream = new MemoryStream(model.ProfileImage); IFormFile file = new FormFile(stream, 0, stream.Length, "file", "profileImage"); if (stream != null) { string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + file.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using var fileStream = new FileStream(filePath, FileMode.Create); file.CopyTo(fileStream); } return(uniqueFileName); }
public static string Save(byte[] fileBytes, string fullPath) { if (fileBytes != null && fileBytes.Length > 0) { var file = new FormFile(null, 0, fileBytes.Length, "", ""); using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } if (File.Exists(fullPath)) { return(fullPath); } } return(null); }
public static string SaveImage(ItemModel item, IWebHostEnvironment webHostEnvironment) { if (item.Photo == null) { return("~/images/no_image.jpg"); } string path = Path.Combine(webHostEnvironment.WebRootPath, "images"); string photoName = item.ID + ".jpg"; path = Path.Combine(path, photoName); if (System.IO.File.Exists(path)) { return("~/images/" + photoName); } var fileStream = new FileStream(path, FileMode.Create); var stream = new MemoryStream(item.Photo); IFormFile Photo = null; try { Photo = new FormFile(stream, 0, item.Photo.Length, photoName, "temp"); Photo.CopyTo(fileStream); fileStream.Close(); } finally { if (stream != null) { stream.Dispose(); } if (fileStream != null) { fileStream.Dispose(); } } return("~/images/" + photoName); }
public IActionResult ReadIFormFile() { string contentRootPath = _hostingEnvironment.ContentRootPath; var fileLocation = Path.Combine(contentRootPath, "Files"); var file = Path.Combine(fileLocation, "Startup.cs"); using (FileStream fileStream = new FileStream(file, FileMode.Open)) { IFormFile fileInfo = new FormFile(fileStream, 0, fileStream.Length, null, Path.GetFileName(fileStream.Name)) { Headers = new HeaderDictionary(), ContentType = MediaTypeNames.Application.Octet }; var newLocation = Path.Combine(contentRootPath, "New"); var filePath = Path.Combine(newLocation, $"New{fileInfo.FileName}"); using (var fileStreamx = new FileStream(filePath, FileMode.Create)) { fileInfo.CopyTo(fileStreamx); } fileStream.Close(); } return(Ok("Copied Successfully")); }
/// <summary> /// 发布作业 /// </summary> /// <returns><c>true</c>, if job was published, <c>false</c> otherwise.</returns> /// <param name="file">form file</param> public bool PublishJobPackage(string clusterName, FormFile file) { // 获取当前Manager var manager = GetManager(clusterName); if (manager == null) { throw new Exception("没有发现在线的Manager"); } #region 检查作业包 // 先保存作业包 string pkgName = file.FileName; string jobName = pkgName.Substring(0, pkgName.LastIndexOf('.')); string uploadJobPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "uploadjobs"); if (!Directory.Exists(uploadJobPath)) { Directory.CreateDirectory(uploadJobPath); } var pkgPath = Path.Combine(uploadJobPath, pkgName); using (var stream = new FileStream(pkgPath, FileMode.Create)) { file.CopyTo(stream); } // 然后解压作业包 string jobPath = Path.Combine(uploadJobPath, jobName); if (Directory.Exists(jobPath)) { // 作业包目录删除重建,以保证文件都是最新的 Directory.Delete(jobPath, true); Directory.CreateDirectory(jobPath); } using (var zip = ZipFile.Open(pkgPath, ZipArchiveMode.Read)) { zip.ExtractToDirectory(jobPath); } // 读取配置文件 var jobConfigPath = Path.Combine(jobPath, "job.json"); var jobConfig = new JobConfig(jobConfigPath); if (string.IsNullOrWhiteSpace(jobConfig.Name) || string.IsNullOrWhiteSpace(jobConfig.FileName) || string.IsNullOrWhiteSpace(jobConfig.JobClassName) || jobConfig.RunTimePlan.Length <= 0) { throw new Exception("作业配置项缺失,请检查作业名称、可执行文件名称、作业入口类、运行时间计划。"); } var exePath = Path.Combine(jobPath, jobConfig.FileName); if (!File.Exists(exePath)) { throw new Exception("作业配置指定的可执行文件不存在。"); } // 设置版本为当前时间 jobConfig.Version = DateTime.Now.ToString("yyyyMMddHHmmss"); // 重新写配置文件 File.WriteAllText(jobConfigPath, JsonConvert.SerializeObject(jobConfig)); // 将新的配置文件打包 using (var zip = ZipFile.Open(pkgPath, ZipArchiveMode.Update)) { var entry = zip.GetEntry("job.json"); entry.Delete(); zip.CreateEntryFromFile(jobConfigPath, "job.json"); } #endregion // 上传到Manager string url = string.Format("{0}upload/job/package?jobName={1}&jobVersion={2}", manager.CommunicationAddress, jobConfig.Name, jobConfig.Version); WebClient client = new WebClient(); client.UploadData(url, File.ReadAllBytes(pkgPath)); return(true); }
public byte[] ConvertToByteArray() { using var ms = new MemoryStream(); FormFile.CopyTo(ms); return(ms.ToArray()); }