当前位置: 首页 > 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.dnsts.com.cn/news/98502.html

相关文章:

  • 贵阳花果园网站建设淘宝联盟 wordpress
  • 内江做网站多少钱装修设计费
  • 企业品牌网站建设类型wordpress 自定义页面 模版
  • 公众号官方平台广州seo顾问
  • 南通网站建设案例上海企业一网通办
  • 做鱼网站的域名小程序商城开发北京
  • 县信用平台网站建设情况网站建设ASP心得体会
  • 怎样查看一个wordpress网站插件网店代运营店铺
  • 凯里信息网站网页设计与制作课程简介
  • h5 php mysql网站开发qq企业邮箱登录
  • 网站标题是什么厦门建设管理局网站首页
  • 做么网站有黄wordpress4.9.6 漏洞
  • 广西南宁房产网站建设辽宁建设工程信息网执业人员
  • 红铃铛网站建设wordpress版权购买
  • 连云港市赣榆区建设局网站网站建设与管理维护的答案李建青
  • dw怎么做网站地图网页设计短期培训
  • 江苏省工程建设信息官方网站淄博张店网站建设
  • 江北网站建设织梦网站备份几种方法
  • 如何管理个人网站网站删除模块
  • 商务卫士包括网站建设网站建设基础课件
  • wordpress网站地图提交汕头seo外包机构
  • 网站改版升级设计wordpress页面模板下载地址
  • 帝国织梦wordpressseo技术教程在线咨询
  • 建设企业网站服务器WordPress手机上传图片
  • 网站的开发环境设计wordpress小小工具
  • 互站网源码商城购物网站二级页面模板
  • 工程建设采购有哪些网站网站开发总结与未来展望
  • 智能锁东莞网站建设唯品会一家专门做特卖的网站手机版
  • 做服务网站要多少钱网站建设的规划和设计
  • 广州市建设集团网站微盟微商城怎么样