我正在尝试将文件上传到FTP ,并且直到今天才成功完成。在这里(在工作中)我从来没有遇到过问题,但在现场制作中我们遇到了零星的问题,但现在今天突然间它不会100%的时间工作而我我无法解决这个问题。
我已经尝试了几乎所有我能找到的东西。
以下是我使用的方法。如果您需要更多信息,请与我们联系。
public static void FtpUpload(
String username,
String password,
String address,
Int32 port,
Boolean usePassive,
String filePath)
{
string ftpServerIP = String.Format("ftp://{0}", address);
string ftpUserID = username;
string ftpPassword = password;
FileInfo fileInf = new FileInfo(filePath);
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerIP);
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = false;
request.UsePassive = false;
request.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
Stream strm = request.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch
{
throw;
}
}
编辑:
此代码snippit有效。请注意,当我更新此片段以使用块时,它会恢复失败。是否可以使用使用块?
{{1}}
答案 0 :(得分:1)
对于任何偶然发现此问题的人,
为我工作,您必须将以下设置为false
class HostComponent {
@ViewChild(TestComponent ) component!: TestComponent ;
}
describe('TestComponent', () => {
let component: TestComponent;
let host: HostComponent;
let TestServiceMock : TestService;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
],
declarations: [
TestComponent,
],
providers: [
{ provide: TestService, useClass: TestServiceMock }
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents().then(() => {
TestServiceMock = TestBed.get(TestService);
fixture = TestBed.createComponent(HostComponent);
host = fixture.componentInstance;
component = host.component;
fixture.detectChanges();
});
}));
it('calls nested Asyc functions', async () => {
responseMock= {
'data' : [];
} // some mock data
spyOn(component,'fun1').and.callThrough();
spyOn(component,'fun2').and.callThrough();
spyOn(TestServiceMock, 'test2').and.returnValue(responseMock);
const data: string[] = ['1f1f221', '1f1f222'];
await component.test1(data);
expect(await TestServiceMock.test2).toHaveBeenCalledWith(data);
fixture.whenStable().then(() => {
expect(component.fun1).toHaveBeenCalled(); // Error here : 'Unhandled Promis rejection:',
//'<toHaveBeenCalled> : Expected a spy, but got Function.
expect(component.fun2).toHaveBeenCalled();
});
});
});
这解决了FTP上传在本地工作且一旦部署到UAT失败的问题