【发布时间】: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 但字符串没有与文件一起传递?请详细说明您的确切问题。
-
我必须将字符串与文件一起传递,如果我现在才回答,对不起。