正在将文档上载到App_Data



我目前有一些工作代码,可以将文档上传到App_data文件,但如果上传的文件名称相同,我需要能够区分它们。我想通过修改文件名这样做:ID"Filename

我曾尝试过将其包含在传递给控制器的对象中,但我找不到它存储在任何地方(我认为它在传递时会被剥离?)。

这是我当前的代码:

var files = $('#txtUploadFile')[0].files;
if (files.length > 0) {
    if (window.FormData !== undefined) {
        var data = new FormData();
        for (var x = 0; x < files.length; x++) {
            data.append("file" + x, files[x]);
        }
        // data.uploadName = task.Id + " " + files[0].name; 
        // File.filename = Id + " " + file.filename;
        $.ajax({
            type: "POST",
            url: '../Document/UploadFiles/',
            contentType: false,
            processData: false,
            //data: {'id': (nextRef + 1), 'fileLocation': files[0].name }, // THIS DOESN'T WORK
            data: data, // THIS WORKS WITHOUT ANY OTHER VARIABLES
            dataType: "json",
            success: function (result) {
                //alert(result);
            },
            error: function (xhr, status, p3, p4) {
                var err = "Error " + " " + status + " " + p3 + " " + p4;
                if (xhr.responseText && xhr.responseText[0] == "{")
                    err = JSON.parse(xhr.responseText).Message;
                alert(log(err));
            }
        });
    } else {
        alert("This browser doesn't support HTML5 file uploads!");
    }
}

[HttpPost]
public JsonResult UploadFiles()//string id, string fileLocation)
{
    try
    {
        foreach (string file in Request.Files)
        {
            var hpf = Request.Files[file] as HttpPostedFileBase;
            if (hpf.ContentLength == 0)
                continue;
            var fileContent = Request.Files[file];
            if (fileContent != null && fileContent.ContentLength > 0)
            {
                // get a stream
                var stream = fileContent.InputStream;
                // and optionally write the file to disk
                var fileName = Path.GetFileName(file);
                var path = Path.Combine(Server.MapPath("~/App_Data/"), Path.GetFileName(hpf.FileName));
                // Save the file
                hpf.SaveAs(path); 
            }
        }
    }
    catch (Exception)
    {
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return this.Json("Upload failed");
    }
    return this.Json("File uploaded successfully");
}

url: '../Document/UploadFiles/',更改为url: '@Url.Action("UploadFiles","YourController")和在你的控制器

public class YourController:Controller{
  [HttpPost]
   publiction ActionResult UploadFiles(){
        if (Request.Files != null && Request.Files.Count > 0)
        {              
          string path=Server.MapPath("~/App_Data");
          Request.Files[0].SaveAs(path + fileName);
      return Json("File uploaded","text/json",JsonRequestBehavior.AllowGet);
        }
      return Json("No File","text/json",JsonRequestBehavior.AllowGet);
   }
}

您的html应该是

<div><input type="file" name="UploadFile" id="fileupload" Class="fileupload"  />
</div> 

您的ajax调用应该是

 $.ajax({
           type: "POST",
           url: '/MyController/UploadFiles?id=' + myID,
           contentType: false,
           processData: false,
           data: data,
           success: function(result) {
               console.log(result);
           },
           error: function (xhr, status, p3, p4){
            });

//和你的控制器

 [HttpPost]
 public JsonResult UploadFiles(string id)  
 {
            // whatever you want to that id
 }

编码快乐。。。

相关内容

  • 没有找到相关文章

最新更新