2014-10-16 135 views
0

我想使用上傳YouTube上的視頻this code文件上傳錯誤的文件路徑

我已經

if (FileUpload1.HasFile) 
{ 
    var filePath = FileUpload1.FileName; // Replace with path to actual movie file. 

    using (var fileStream = new FileStream(filePath, FileMode.Open)) 
    { 
     var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*"); 
     videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged; 
     videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived; 

     await videosInsertRequest.UploadAsync(); 
    } 
} 

改變var filePath = @"REPLACE_ME.mp4";但我發現了一個錯誤,這是錯的路徑 enter image description here

我該如何解決這個問題? 另一個我曾嘗試:

var filePath = FileUpload1.PostedFile.FileName; 
var filePath = FileUpload1.PostedFile.ToString(); 
var filePath = Path.GetFileName(FileUpload1.FileName); 

但同樣的結果..

+0

如果您如果不提供完整路徑,系統將使用當前運行在C:\ Program Files(x86)\ IIS Express上的應用程序的當前路徑,這顯然是IIS Express。解決方案:在保存之前提供完整路徑。 – 2014-10-16 12:03:53

回答

2

您可能需要先保存文件中使用:

var newFile = Server.MapPath(FileUpload1.FileName); 
FileUpload1.SaveAs(newFile); 

或者像你與FileStream類的工作,你總是可以嘗試:

FileUpload1.FileContent; // gets the file as a stream 
1

我認爲你必須這樣做:

Server.MapPath(FileUpload1.FileName); 

Controler.Server Property

編輯:

或只使用像這樣的流如果你不一定要這樣做localy AVE文件:

youtubeService.Videos.Insert(video, "snippet,status", FileUpload1.FileContent, "video/*"); 
+0

同樣的結果。而我怎麼知道Server.MapPath是用於服務器中的文件。我想通過客戶端上傳文件 – gsiradze 2014-10-16 11:58:20

+0

哦,我很抱歉。如果你只是想獲得文件流,我會用這種方式使用FileUpload1.FileContent屬性,你不必首先保存文件locale。 – t3chb0t 2014-10-16 12:06:30