如何使用angularjs和MVC3从同一函数中保存两个不同的html文件上传两个文件

时间:2015-12-02 12:49:13

标签: html angularjs html5 asp.net-mvc-3

这是我的所有代码我试图上传小图像和大图像分开但angularjs不让我允许这样做,它只采取一个文件,但没有采取其他一个。任何人都可以提供帮助。提前谢谢。

<!-- type definition -->
<fieldType name="text" class="solr.TextField" positionIncrementGap="100">
  <analyzer> 
    <tokenizer class="solr.StandardTokenizerFactory"/>
    <filter class="solr.ASCIIFoldingFilterFactory" preserveOriginal="false"/>
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
</fieldType>

1 个答案:

答案 0 :(得分:0)

您要将文件附加到formdata,因此您需要将ThumbnailLargeImage指定为MVC控制器的参数。请参阅以下内容:

    [HttpPost]
    public JsonResult SaveFiles(
          HttpPostedFileBase thumbnail
        , HttpPostedFileBase largeImage
        , string titleHeading
        , string topic)
    {
        string Message, fileName, actualFileName;
        Message = fileName = actualFileName = string.Empty;
        bool flag = false;

        if (thumbnail != null && thumbnail.ContentLength != 0) 
        {
            SaveFile(thumbnail);
        }

        if (largeImage != null && largeImage.ContentLength != 0)
        {
            SaveFile(largeImage);
        }

        return new JsonResult { Data = new { Message = Message, Status = flag } };
    }

    private void SaveFile(
        HttpPostedFileBase httpFile) 
    {
        var actualFileName = httpFile.FileName;
        var fileName = Guid.NewGuid() + Path.GetExtension(httpFile.FileName);
        int size = httpFile.ContentLength;
        try
        {
            httpFile.SaveAs(Path.Combine(Server.MapPath("~/UploadedFiles"), fileName));
            using (TCDataClassesDataContext dc = new TCDataClassesDataContext())
            {
                Article insert = new Article();
                insert.ArticleId = Guid.NewGuid();
                insert.TitleHeading = TitleHeading;
                insert.SmallImagePath = fileName;
                dc.Articles.InsertOnSubmit(insert);
                dc.SubmitChanges();
                Message = "File uploaded successfully";
                flag = true;
            }
        }
        catch (Exception)
        {
            Message = "File upload failed! Please try again";
        }
    }