【问题标题】:How can I send a file and a string to web api?如何将文件和字符串发送到 web api?
【发布时间】:2020-01-18 09:31:21
【问题描述】:

我创建了一个文件上传服务(asp.net),同时输入插入文件的用户名(按照下面的代码),现在我必须创建一个C# 客户端来上传文件,你能帮帮我?

        public class FIleUploadAPI
        {
            public IFormFile files { get; set; }
        }

        [HttpPost]
        public async Task<string> Post(FIleUploadAPI files, string nu)
        {
            if (files.files.Length > 0)
            {
                try
                {
                    if (!Directory.Exists(_environment.WebRootPath + "/uploads/"))
                    {
                        Directory.CreateDirectory(_environment.WebRootPath + "uploads");
                    }
                    using (FileStream filestream = System.IO.File.Create(files.files.FileName))
                    {
                        files.files.CopyTo(filestream);
                        filestream.Flush();
                        string[] pht = files.files.FileName.Split(".");
                        filestream.Close();
                        if (!Directory.Exists(_environment.WebRootPath + "/uploads/"))
                        {
                            Directory.CreateDirectory(_environment.WebRootPath + "uploads/" + nu);
                        }
                        DateTime now = DateTime.Now;
                        System.IO.File.Move(files.files.FileName, @"uploads/" + nu + "/" + pht[0] + "_" + now.ToString("MMddyyyyHHmmss") + "." + pht[1]);
                        return "/uploads/" + nu + "/" + pht[0] + "_" + now.ToString("MMddyyyyHHmmss") + "." + pht[1];
                    }
                }
                catch (Exception ex)
                {
                    return ex.ToString();
                }
            }
            else
            {
                return "Unsuccessful";
            }

        }

【问题讨论】:

  • 请描述什么不起作用,I have to create a C # client to upload the file, can you help me? 不是我们可以在这里提供帮助的问题,它太宽泛了。您是否看到了错误,什么不起作用,预期的输出/行为是什么?
  • 我建议忽略你对类、属性等的命名。如果命名没有清楚地表明它的目的,它会给你自己和其他试图理解你的代码和它的目的的人造成混乱。
  • 您需要将文件与字符串一起传递吗?或文件正在进入 web api 但字符串没有与文件一起传递?请详细说明您的确切问题。
  • 我必须将字符串与文件一起传递,如果我现在才回答,对不起。

标签: c# asp.net .net upload


【解决方案1】:

        [HttpPost]
        [Route(UploadFile)]
        public async Task<string> Post(IFormFile files, string nu)
        {
            if (files.Length > 0)
            {
                try
                {
                    if (!Directory.Exists(_environment.WebRootPath + "/uploads/"))
                    {
                        Directory.CreateDirectory(_environment.WebRootPath + "uploads");
                    }
                    using (FileStream filestream = System.IO.File.Create(files.FileName))
                    {
                        files.CopyTo(filestream);
                        filestream.Flush();
                        string[] pht = files.FileName.Split(".");
                        filestream.Close();
                        if (!Directory.Exists(_environment.WebRootPath + "/uploads/"))
                        {
                            Directory.CreateDirectory(_environment.WebRootPath + "uploads/" + nu);
                        }
                        DateTime now = DateTime.Now;
                        System.IO.File.Move(files.FileName, @"uploads/" + nu + "/" + pht[0] + "_" + now.ToString("MMddyyyyHHmmss") + "." + pht[1]);
                        return "/uploads/" + nu + "/" + pht[0] + "_" + now.ToString("MMddyyyyHHmmss") + "." + pht[1];
                    }
                }
                catch (Exception ex)
                {
                    return ex.ToString();
                }
            }
            else
            {
                return "Unsuccessful";
            }

        }`enter code h


var formData = new FormData();
var file = document.getElementById("FileUpload").files[0];
formData.append('FileUpload', file);

$.ajax({
    url: "/controllername/UploadFile? nu=" + "mystring",
    dataType: 'json',
    type: 'post',
    data: formData,
    processData: false,
    contentType: false,

    success: function (result) {
    },

    error: function () {
    }
});

【讨论】:

  • 请更新您的帖子以包含对您提出的解决方案的解释。目前它没有解释它的作用,通过提供一个解释它将帮助 OP 理解以及可能遇到此解决方案的任何其他人;也请格式化您的帖子。
  • @Sridhar R 不明白应该使用库以及这段代码实际上做了什么我不明白它是如何工作的,它给了我很多错误
  • 我输入了你的代码,但是没有找到 FormData() 和 document.getElementById 和 $.ajax
  • FormData 是 jquery 功能,我已经使用了上面的代码,我可以上传文件。 var formData = new FormData(); var file = document.getElementById("FileUpload").files[0]; formData.append('FileUpload', 文件);
猜你喜欢
  • 1970-01-01
  • 2016-08-02
  • 2017-09-10
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
  • 2012-05-11
  • 2017-01-17
  • 2017-03-02
相关资源
最近更新 更多