Ciao a tutt
qualche settimana fa avevo aperto una discussione sull'upload di video su youtube da un pannello di controllo utilizzando le api. Per caricare i video utilizzavo uplodify e quando provavo a caricare file di grandi dimensioni(avevo fatto test con file di 78 e 110 mega), uplodify mi restituiva errore.
Non so spiegarmi come, ma lo stesso codice copiato in un'altro progetto ora funziona.
Ora ho questo problema: seleziono il file di grandi dimensioni, carico il file con uplodify, il file viene caricato su youtube e la mia applicazione rimane in attesa che youtube restituisca il videoID assegnato al video. Il problema è che dopo un pò di tempo l'esecuzione del codice si blocca e non mi restituisce il codice associato al video, ma il video è presente su youtube. Dato che devo salvare su un database il codice. questo è un problema.
Perchè accade questo?
Come posso caricare un file di grandi dimensioni senza utilizzare uplodify?
A qualcuno è mai capitato di carica un file di grandi dimensioni con le api di youtube?
Quì trovate un pò di codice:
#MASTERPAGE#
<form id="frmMaster" method="post" runat="server" enctype="multipart/form-data">
<asp:ToolkitScriptManager ID="smAdmin" AsyncPostBackTimeout="999999" EnableScriptGlobalization="true" EnableScriptLocalization="true" EnablePageMethods="true" runat="server"></asp:ToolkitScriptManager>
#VIDEO.ASPX#
<!-- Video -->
<script type="text/javascript">
$(function () {
$("#fuVideo").uploadify({
'fileSizeLimit' : '0',
'swf': '/Scripts/Uploadify/uploadify.swf',
'uploader': '/Handler/FileUpload.ashx',
'fileTypeDesc' : 'Video Files',
'fileTypeExts' : '<%= YouTube.VIDEO_FORMAT %>',
'queueSizeLimit' : 1,
'removeTimeout' : 1,
'multi': false,
'auto': false,
'successTimeout': 36000,
'onUploadSuccess' : function(file, data, response) {
$('#loading').hide();
if(response)
{
var obj = jQuery.parseJSON(data);
if(obj.msg_error == '')
{
$('#<%= hdYouTube.ClientID %>').val('[{"Key" : "VideoId", "Value" : "' + obj.id_item + '" },{"Key" : "Image", "Value" : "' + obj.filename + '"}]');
var operazione = $('#<%= hdOperazione.ClientID %>').val();
switch(operazione)
{
case "applica":
$('#<%= btnApplica.ClientID %>').click();
break;
case "salva":
$('#<%= btnSalva.ClientID %>').click();
break;
}
}
else
{
$('#loading').hide();
alert(obj.msg_error);
e.preventDefault();
}
}
else
{
$('#loading').hide();
alert('<%= Messaggi.ERR_UPLOAD_VIDEO %>');
e.preventDefault();
}
},
'onUploadError' : function(file, errorCode, errorMsg, errorString) {
$('#loading').hide();
alert('<%= Messaggi.ERR_UPLOAD_VIDEO %>');
e.preventDefault();
},
'onUploadStart' : function(file) {
$('#fuVideo').uploadify('settings','formData',{
'type' : 'video_news',
'title' : $('#<%= txtTitolo.ClientID %>').val(),
'title_old' : $('#<%= hdTitleOld.ClientID %>').val(),
'category' : $('#<%= ddlCategoria.ClientID %>').val(),
'description' : $('#<%= txtDescription.ClientID %>').val(),
'keywords' : $('#<%=txtKeywords.ClientID %>').val()
});
}
});
});
</script>
<!-- fine Video -->
#FILEUPLOAD.ASHX#
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Expires = -1;
string type = context.Request["type"];
string id = context.Request["id"];
string result = string.Empty;
if (context.Request.Files.Count == 0)
{
context.Response.ContentType = "text/plain";
context.Response.Write("{\"msg_error\":\"" + Messaggi.ERR_NO_FILE + "\"}");
}
else
{
HttpPostedFile uploadedfile = context.Request.Files["Filedata"];
if (uploadedfile != null && uploadedfile.ContentLength > 0)
{
string FileName = uploadedfile.FileName;
string FileType = uploadedfile.ContentType;
int FileSize = uploadedfile.ContentLength;
string id_item = "0";
string name = string.Empty;
string filename = string.Empty;
string link = string.Empty;
string error = string.Empty;
List<string> msg_error = new List<string>();
List<string> param = new List<string>();
LogRequest(FileName + ", " + FileType + ", " + FileSize);
switch (type)
{
case "video_news":
string _title = context.Request["title"];
string _title_old = context.Request["title_old"];
string _category = context.Request["category"];
_category = _category;
string _description = context.Request["description"];
string _keywords = context.Request["keywords"];
tipoYouTubeVideo _video = new tipoYouTubeVideo();
_video.Title = _title;
_video.TitleOld = _title_old;
_video.Category = _category;
_video.Description = _description;
_video.Keywords = _keywords;
_video.Upload = uploadedfile;
if (News.salvaVideo(_video, out msg_error))
{
id_item = _video.VideoId;
name = _title;
filename = _video.Image;
link = _video.Watch;
}
else
{
msg_error.ForEach(item => error += (!string.IsNullOrEmpty(error) ? "\r\n" : string.Empty) + item);
}
break;
default:
break;
}
if (id_item != "0" && id_item != string.Empty)
result = "{\"id_item\":\"" + id_item + "\",\"name\":\"" + name + "\",\"filename\":\"" + filename + "\",\"link\":\"" + link + "\",\"msg_error\":\"" + error + "\"}";
else
result = "{\"id_item\":\"\",\"name\":\"\",\"filename\":\"\",\"link\":\"\",\"msg_error\":\"" + error + "\"}";
}
else
{
result = "{\"msg_error\":\"" + Messaggi.ERR_DESC_ZERO_LENGTH_FILE + "\"}";
}
context.Response.ContentType = "text/plain";
context.Response.Write(result);
}
}
#YOUTUBE.CS#
public static bool salvaVideo(tipoYouTubeVideo video, out List<string> msg_error)
{
msg_error = new List<string>();
try
{
bool errore = false;
string filename = video.Upload.FileName;
string FileType = video.Upload.ContentType;
string videoname = Utility.PERCORSO_FISICO(VIDEO_TEMP_FOLDER + video.Codice + "_" + Formattazione.EliminaCaratteriSpeciali(filename, "_"));
if (!errore)
{
video.Upload.SaveAs(videoname);
//Autenticazione
YouTubeRequest request = new YouTubeRequest(settings);
((GDataRequestFactory)request.Service.RequestFactory).Timeout = 9999999;
Video newVideo = new Video();
newVideo.Title = Formattazione.SottoStringa(Formattazione.FormatString(System.Web.HttpUtility.HtmlDecode(video.Title)), TITLE_MAX_CHARACTER, false);
newVideo.Tags.Add(new MediaCategory(video.Category, YouTubeNameTable.CategorySchema));
newVideo.Keywords = System.Web.HttpUtility.HtmlDecode(video.Keywords);
newVideo.Description = Formattazione.SottoStringa(Formattazione.FormatString(System.Web.HttpUtility.HtmlDecode(video.Description)), DESCRIPTION_MAX_CHARACTER, false);
newVideo.YouTubeEntry.Private = true;
newVideo.Tags.Add(new MediaCategory(video.DeveloperTag, YouTubeNameTable.DeveloperTagSchema));
newVideo.YouTubeEntry.setYouTubeExtension("location", "Andria, BT");
newVideo.YouTubeEntry.MediaSource = new MediaFileSource(videoname, FileType);
Video createdVideo = request.Upload(newVideo);
if (createdVideo != null)
{
video.VideoId = createdVideo.VideoId;
video.Image = createdVideo.Thumbnails[0].Url;
}
//Elimino il video il locale
IO.DeleteFile(videoname);
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
msg_error.Add(Messaggi.ERR_UPLOAD_VIDEO);
if (!string.IsNullOrEmpty(video.VideoId)) YouTube.Elimina(video.VideoId);
return false;
}
}
Esegue Video createdVideo = request.Upload(newVideo);, carica il video su youtube, ma poi non va avanti.
#WEB.CONFIG#
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="ajaxNet">
<section name="ajaxSettings" type="AjaxPro.AjaxSettingsSectionHandler,AjaxPro.2" requirePermission="false" restartOnExternalChanges="true"/>
</sectionGroup>
</configSections>
<appSettings>
</appSettings>
<connectionStrings>
</connectionStrings>
<system.web>
<globalization enableClientBasedCulture="true" culture="auto" uiCulture="auto:it-IT" fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" responseHeaderEncoding="utf-8" enableBestFitResponseEncoding="true"/>
<pages enableEventValidation="false" viewStateEncryptionMode="Never" enableViewStateMac="false" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" validateRequest="false">
<controls>
<add tagPrefix="CKEditor" namespace="CKEditor.NET" assembly="CKEditor.NET"/>
</controls>
</pages>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.Services.Client, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.Services.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.RegularExpressions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="~/Admin/login.aspx" protection="Validation" timeout="999999" defaultUrl="~/Admin/default.aspx"/>
</authentication>
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.aspx">
<error statusCode="403" redirect="NoAccess.aspx"/>
<error statusCode="404" redirect="FileNotFound.aspx"/>
</customErrors>
<sessionState mode="InProc"/>
<httpRuntime maxRequestLength="2097151" requestLengthDiskThreshold="999999" executionTimeout="999999" enableHeaderChecking="false" enable="true"/>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<location path="ajaxpro">
<system.web>
<httpHandlers>
<add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
</httpHandlers>
</system.web>
</location>
</configuration>
Grazie mille