[英]FTP Server File Upload Cdn Ef Core
CDN is installed in Local FTP Server. CDN安装在本地FTP服务器中。 I want to upload file to it but it gets the file path wrong.
我想将文件上传到它,但它的文件路径错误。 I think it is due to Handler but I couldn't find the error.
我认为这是由于 Handler 但我找不到错误。
File Upload;上传文件;
public static class FileUpload
{
public static string ImageUpload(this IFormFile file, string path)
{
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(@"cdnadress" + "/" + Path.GetFileName(path));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("username", "password");
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true;
//Load the file
FileStream stream = File.OpenRead(path);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
//Upload file
Stream reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
request = null;
return null;
}
CreateHandler;创建处理程序;
public class SliderCreateHandler : IRequestHandler<SliderCreate, ApiResponse<SliderResponse>>
{
private readonly IUnitOfWork _repo;
private readonly IMapper _mapper;
public SliderCreateHandler(IUnitOfWork repo, IMapper mapper)
{
_repo = repo;
_mapper = mapper;
}
public async Task<ApiResponse<SliderResponse>> Handle(SliderCreate request, CancellationToken cancellationToken)
{
var mapped = _mapper.Map<Slider>(request);
mapped.ImageUrl = request.file.ImageUpload(Environment.CurrentDirectory+ @"\Image\Slider\");
if (string.IsNullOrEmpty(mapped.ImageUrl))
return new ErrorApiResponse<SliderResponse>(ResultMessage.UnsupportedFileType);
if (mapped == null)
return new ErrorApiResponse<SliderResponse>(ResultMessage.NotCreatedSlider);
var model = await _repo.Sliders.AddAsync(mapped);
var response = _mapper.Map<SliderResponse>(model);
return new SuccessApiResponse<SliderResponse>(response);
}
} }
Here is the error id.这是错误ID。 I was racing on this road somewhere or I missed it.
我在某处的这条路上赛车,或者我错过了它。 Can you help me?
你能帮助我吗?
If your goal was to upload the IFormFile
then:如果您的目标是上传
IFormFile
,那么:
IFormFile.CopyTo
to access the file contents.IFormFile.CopyTo
来访问文件内容。 It's all in-memory data.IFormFile.FileName
to find the name of the file.IFormFile.FileName
查找文件的名称。 Some fallback name is recommendable, as the browser might not provide one.FtpWebRequest request =
(FtpWebRequest)FtpWebRequest.Create(@"cdnadress" + "/" + file.FileName);
// rest of your FtpWebRequest setup here
using (Stream reqStream = request.GetRequestStream())
{
file.CopyTo(reqStream);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.