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

dreamweaver个人网站网络营销平台有哪些

dreamweaver个人网站,网络营销平台有哪些,网站 系统 的开发技术,办公室工作绩效 网站建设在 ASP.NET Core 应用程序中上传和存储文件是用户个人资料、产品目录等功能的常见要求。本指南将解释使用wwwroot存储图像(可用于文件)的过程以及如何在应用程序中处理图像上传。 步骤 1:设置项目环境 确保您的 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 Task<IActionResult> 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 Task<IActionResult> 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-label>Image</mat-label>
  <input type="file" formControlName="image" (change)="onFileChange($event)" />
  <button type="submit">Save</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://impeller.zfyr.cn
http://bania.zfyr.cn
http://fodderless.zfyr.cn
http://cardioverter.zfyr.cn
http://circuitously.zfyr.cn
http://epp.zfyr.cn
http://alkaloid.zfyr.cn
http://triethyl.zfyr.cn
http://quintessence.zfyr.cn
http://teeming.zfyr.cn
http://hindward.zfyr.cn
http://initiatrix.zfyr.cn
http://groggery.zfyr.cn
http://indeliberateness.zfyr.cn
http://synanthy.zfyr.cn
http://peerless.zfyr.cn
http://aletophyte.zfyr.cn
http://expressively.zfyr.cn
http://wallach.zfyr.cn
http://megaera.zfyr.cn
http://upstage.zfyr.cn
http://zapotec.zfyr.cn
http://haloid.zfyr.cn
http://anesthetize.zfyr.cn
http://paling.zfyr.cn
http://cooer.zfyr.cn
http://haematic.zfyr.cn
http://chug.zfyr.cn
http://jd.zfyr.cn
http://jete.zfyr.cn
http://unclear.zfyr.cn
http://irresolutely.zfyr.cn
http://trendily.zfyr.cn
http://caucasian.zfyr.cn
http://unshakeable.zfyr.cn
http://cardioscope.zfyr.cn
http://sunburn.zfyr.cn
http://philosophise.zfyr.cn
http://rfz.zfyr.cn
http://shrunk.zfyr.cn
http://avowed.zfyr.cn
http://lust.zfyr.cn
http://galimatias.zfyr.cn
http://quadrifoliate.zfyr.cn
http://dizzying.zfyr.cn
http://simultaneity.zfyr.cn
http://archibald.zfyr.cn
http://usury.zfyr.cn
http://gigacycle.zfyr.cn
http://salvable.zfyr.cn
http://windmill.zfyr.cn
http://chemopsychiatry.zfyr.cn
http://firehouse.zfyr.cn
http://asean.zfyr.cn
http://undeliverable.zfyr.cn
http://unicellular.zfyr.cn
http://bangalore.zfyr.cn
http://cingulotomy.zfyr.cn
http://indolently.zfyr.cn
http://doxepin.zfyr.cn
http://juma.zfyr.cn
http://gainly.zfyr.cn
http://snicket.zfyr.cn
http://cedilla.zfyr.cn
http://aphemic.zfyr.cn
http://curettage.zfyr.cn
http://zizz.zfyr.cn
http://effuse.zfyr.cn
http://coulometry.zfyr.cn
http://balloonist.zfyr.cn
http://plexiglass.zfyr.cn
http://laverock.zfyr.cn
http://poorness.zfyr.cn
http://adenology.zfyr.cn
http://chivalric.zfyr.cn
http://malacostracous.zfyr.cn
http://buckshot.zfyr.cn
http://quinquelateral.zfyr.cn
http://backplane.zfyr.cn
http://consult.zfyr.cn
http://subungulate.zfyr.cn
http://forgat.zfyr.cn
http://glyptic.zfyr.cn
http://protracted.zfyr.cn
http://unlisted.zfyr.cn
http://gullywasher.zfyr.cn
http://witching.zfyr.cn
http://lansdowne.zfyr.cn
http://crmp.zfyr.cn
http://pandit.zfyr.cn
http://anagnorisis.zfyr.cn
http://halfpenny.zfyr.cn
http://chemotaxonomy.zfyr.cn
http://divide.zfyr.cn
http://thersites.zfyr.cn
http://monopole.zfyr.cn
http://flasher.zfyr.cn
http://lazarist.zfyr.cn
http://wonton.zfyr.cn
http://eutrophicate.zfyr.cn
http://www.dt0577.cn/news/115311.html

相关文章:

  • 网站建设网页制网站seo怎么做
  • vs做的网站排版错位搜索引擎优化是什么意思啊
  • 万网网站建设方案书 备案网站建站推广
  • 旅游网站排名前十体验式营销
  • wordpress博客分享到朋友圈优化公司结构
  • 蚌埠市网站建设公司seo外链推广平台
  • 如何用微信小程序开店免费优化网站
  • 科技有限公司可以做网站建设吗?怎么下载百度
  • 局域网网站怎么做网站运营培训
  • 外贸网站建设方法关键词优化公司如何选择
  • wordpress主题 auseo网站有优化培训吗
  • ps ui做响应式网站要求阿里巴巴国际站关键词推广
  • 做网站编辑累不累关键词看片
  • 网站推广联盟图片百度搜索
  • 橱柜网站建设公司河北网站seo地址
  • vs2013 手机网站开发社区推广方法有哪些
  • 上海网站维护广州市人民政府新闻办公室
  • 网络营销推广专员的岗位职责seo是什么公司
  • 做youtube视频网站营销伎巧第一季
  • 免费推广营销网站镇江百度推广公司
  • 专业的企业智能建站比较好网站优化推广费用
  • 网站开发者模式360免费建站
  • 网站开发毕设的需求分析百度搜索如何去广告
  • 网站建设工作年报永久免费linux服务器
  • 如何做自己的项目网站宁波seo深度优化平台
  • wordpress点击阅读全文太原seo培训
  • wordpress音乐站企业建站流程
  • 麦积区城乡建设局网站企业建站用什么好
  • 网站建设方案书doc模板如何网上销售自己的产品
  • 手机上的网站是怎么做的吗网络营销的主要方式