异步图像文件保存

时间:2016-01-24 20:32:26

标签: c# asynchronous

在网络mvc项目中。

我有以下功能我试图使异步,因为我保存了很多文件,它有点慢。我是否需要添加到任务列表并等待所有任务:

   var fs = new FileSaver();
        property.FloorPlanPath = fs.SaveFile(propertyRentalForm.FloorPlanFile, "~/uploads/properties/floorplans");
        property.FloorPlan2Path = fs.SaveFile(propertyRentalForm.FloorPlan2File, "~/uploads/properties/floorplans");
        property.EpcPath = fs.SaveFile(propertyRentalForm.EpcFile, "~/uploads/properties/epc");

    public string SaveFile(HttpPostedFileBase file, string uploadPath)
    {

        if (file == null || file.ContentLength <= 0) return "";
        var fileName1 = Path.GetFileName(file.FileName);
        if (fileName1 == null) return "";
        // clean file name
        fileName1 = string.Format("{0}{1}",
            Path.GetFileNameWithoutExtension(fileName1).GetAliasTitle(),
            Path.GetExtension(fileName1));

        var path = Path.Combine(HttpContext.Current.Server.MapPath(uploadPath), fileName1);
        file.SaveAs(path);
        return fileName1;
    }

1 个答案:

答案 0 :(得分:0)

如果您有单个磁盘来保存这些文件,则没有任何意义使其异步。因为您受磁盘I / O的限制。但是您可以使用队列机制,并且可以异步检查此队列状态...