gpt4 book ai didi

C#上传不带扩展名的文件

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:08 25 4
gpt4 key购买 nike

我有以下代码,我需要上传多个没有分配扩展名/类型的文件。下面的代码有效,但不适用于这些文件,因为它试图创建一个文件,例如:ftp.server/file

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String sourcefilepath = @"path"; // e.g. “d:/test.docx”
String ftpurl = @"ftp"; // e.g. ftp://serverip/foldername/foldername
String ftpusername = @"user"; // e.g. username
String ftppassword = @"pass"; // e.g. password

string[] filePaths = Directory.GetFiles(sourcefilepath);

// Get the object used to communicate with the server.
foreach (string filename in filePaths)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpurl + Path.GetFileName(filename));
request.Method = WebRequestMethods.Ftp.UploadFile;

// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(ftpusername, ftppassword);

// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(sourcefilepath);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

response.Close();
}
}
}
}

文件来自 iPhone 备份,iPhone 备份不提供文件扩展名,因此我需要解决。

最佳答案

我找到的解决方案是复制文件并添加虚拟扩展名。

关于C#上传不带扩展名的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com