当前位置: 首页 > news >正文

太原搭建网站的公司哪家好建筑总包二级资质

太原搭建网站的公司哪家好,建筑总包二级资质,深圳工程建设交易服务中心网站,福安做网站最好在 ASP.NET Core 应用程序中上传和存储文件是用户个人资料、产品目录等功能的常见要求。本指南将解释使用wwwroot存储图像#xff08;可用于文件#xff09;的过程以及如何在应用程序中处理图像上传。 步骤 1#xff1a;设置项目环境 确保您的 ASP.NET 项目中具有必要的依…在 ASP.NET Core 应用程序中上传和存储文件是用户个人资料、产品目录等功能的常见要求。本指南将解释使用wwwroot存储图像可用于文件的过程以及如何在应用程序中处理图像上传。 步骤 1设置项目环境 确保您的 ASP.NET 项目中具有必要的依赖项和环境设置。这包括配置服务和wwwroot在项目中创建用于静态文件服务的文件夹。  静态文件服务是将未编译的内容如图像、CSS 和 JavaScript 文件直接从服务器传送到客户端浏览器的过程。 第 2 步定义模型和 DTO 创建模型和数据传输对象 (DTO) 来处理图像元数据和其他相关数据。  public class Branch {     public Guid Id { get; set; }     public string Name { get; set; }     public string Location { get; set; }     public string Email { get; set; }     public string Phone { get; set; }     public string? ImagePath { get; set; } // Path to the stored image }  步骤 3配置图像上传的控制器操作  实现控制器操作来处理上传图像的 HTTP 请求。  上传并存储图像 1、为图像生成一个唯一的文件名。 2、将图像存储在wwwroot/images目录中或任何其他目录中。 3、在数据库中保存图像路径。 [HttpPost] public async TaskIActionResult AddBranch([FromForm] AddBranchDto addBranchDto, IFormFile image) {     string? imagePath null;     if (image ! null)     {         var fileName ${Guid.NewGuid()}-{Path.GetFileName(image.FileName)};         var filePath Path.Combine(_webHostEnvironment.WebRootPath, images, fileName); using (var stream new FileStream(filePath, FileMode.Create))         {             await image.CopyToAsync(stream);         } imagePath Path.Combine(images, fileName);     } var branchEntity new Branch     {         Name addBranchDto.Name,         Location addBranchDto.Location,         Email addBranchDto.Email,         Phone addBranchDto.Phone,         ImagePath imagePath     }; _dbContext.Branches.Add(branchEntity);     await _dbContext.SaveChangesAsync(); return Ok(branchEntity); } 更新图像 处理图像的更新包括在上传新图像时删除旧图像。 [HttpPut({id:guid})] public async TaskIActionResult UpdateBranch(Guid id, [FromForm] UpdateBranchDto updateBranchDto, IFormFile image) {     var branch _dbContext.Branches.Find(id);     if (branch null)         return NotFound(); if (image ! null)     {         var fileName ${Guid.NewGuid()}-{Path.GetFileName(image.FileName)};         var filePath Path.Combine(_webHostEnvironment.WebRootPath, images, fileName); if (!string.IsNullOrEmpty(branch.ImagePath))         {             var oldFilePath Path.Combine(_webHostEnvironment.WebRootPath, branch.ImagePath.Replace(/, \\));             if (System.IO.File.Exists(oldFilePath))             {                 System.IO.File.Delete(oldFilePath);             }         } using (var stream new FileStream(filePath, FileMode.Create))         {             await image.CopyToAsync(stream);         } branch.ImagePath Path.Combine(images, fileName);     } branch.Name updateBranchDto.Name;     branch.Location updateBranchDto.Location;     branch.Email updateBranchDto.Email;     branch.Phone updateBranchDto.Phone; await _dbContext.SaveChangesAsync(); return Ok(branch); } 步骤 4提供静态文件  配置应用程序以从wwwroot目录提供静态文件。 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {     if (env.IsDevelopment())     {         app.UseDeveloperExceptionPage();     }     else     {         app.UseExceptionHandler(/Home/Error);         app.UseHsts();     } app.UseHttpsRedirection();     app.UseStaticFiles(); // Enable static file serving app.UseRouting();     app.UseAuthorization(); app.UseEndpoints(endpoints     {         endpoints.MapControllers();     }); } 步骤 5前端集成 在您的前端例如Angular中实现表单来处理图像上传。 form [formGroup]branchForm (ngSubmit)onSubmit()   !-- Other form fields --   mat-labelImage/mat-label   input typefile formControlNameimage (change)onFileChange($event) /   button typesubmitSave/button /form 处理文件变更及提交 onFileChange(event: any): void {     const file event.target.files[0];     if (file) {         this.selectedFile file;     } } onSubmit(): void {     if (this.branchForm.valid) {         const formData new FormData();         Object.keys(this.branchForm.value).forEach(key {             formData.append(key, this.branchForm.value[key]);         }); if (this.selectedFile) {             formData.append(image, this.selectedFile);         } if (this.isEditMode) {             this.branchService.updateBranch(this.branchId, formData).subscribe();         } else {             this.branchService.addBranch(formData).subscribe();         }     } } 结论 通过遵循这些步骤您可以成功地将具有唯一名称的图像上传并存储在wwwrootASP.NET 应用程序的目录中从而确保有效地管理和检索图像。 如果您喜欢此文章请收藏、点赞、评论谢谢祝您快乐每一天。
http://www.hkea.cn/news/14586925/

相关文章:

  • 做配件出口上什么网站crazyuncle WordPress
  • 精品网站建设免费seo工具
  • 蜘蛛爬网站泰国公共建设网站
  • 做五金的外贸网站有哪些亚马逊云服务 网站建设
  • 网站建设分哪几种石家庄网站开发
  • centos6.6做网站建设银行关方网站
  • 网站开发前景好吗网站模板建站教程视频
  • 网站目录遍历如果网站没有icp备案号
  • 酒店网站建设自建网站网址
  • 微软网站开发软件高端网约车
  • 重庆市建设公共资源交易中心网站网站cname解析
  • 本地安装wordpress账户是什么搜索引擎优化指的是什么
  • 网站开发手机充值接口宛城区网站建设
  • 学生求职网站的需求分析怎么做大丰做网站
  • wap网站发布西安网站托管专业公司
  • 济南大型网站设计公司网上购物平台哪家质量最好
  • 中国建设人才服务信息网站网站设计规划书例子
  • 莆田仿站定制模板建站wordpress最简单主题
  • 网站开发协议模版智联招聘网站怎么做两份简历模板
  • 天津网站建设技术韶关市住房和城乡建设部网站
  • 大气学校网站安装wordpress用什么
  • 织梦门户网站源码下载移动电商网站建设
  • 成都私人视频网站制作平台网站建设高端
  • 免费网站制作网站源码如何在网站上推广自己的链接
  • js模板网站邯郸怎么读
  • 免费云服务器网站有哪些搜索引擎推广网站
  • 网站的困难免费模板做网站
  • 农村pc网站开发wordpress页面突然不能访问
  • 郑州网站建设项目网站开发vue版本是什么
  • 企业制作网站哪家好网页传奇3