Проблема:
При попытке загружаю файл с удаленного сервера в IIS, получаю сообщение «Сайт недоступен». Журналы ошибок не создаются, и в средстве просмотра событий нет необычных записей. Функция загрузки из IIS работает должным образом.
C#
Код: Выделить всё
public HttpResponseMessage DownloadFile(string networkPath, string documentFullPath, NetworkCredential credentials, string documentPath, string contentType)
{
FileStream stream;
if (isServer.ToLower() == "true")
{
logger.Error("ClinicalEventControll:4710");
using (new ConnectToSharedFolder(networkPath, credentials))
{
logger.Error(":4712");
stream = new FileStream(networkPath + @"\" + documentFullPath, FileMode.Open);
httpResponseMessage.Content = new StreamContent(stream);
httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
httpResponseMessage.Content.Headers.ContentDisposition.FileName = documentPath;
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
httpResponseMessage.Content.Headers.ContentLength = stream.Length;
}
}
return httpResponseMessage;
}
Код: Выделить всё
public class ConnectToSharedFolder : IDisposable
{
private readonly string _networkName;
private static readonly ILog logger = LogManager.GetLogger(typeof(ClinicalEventdetailsApiController));
public ConnectToSharedFolder(string networkName, NetworkCredential credentials)
{
_networkName = networkName;
var netResource = new NetResource
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName
};
var userName = string.IsNullOrEmpty(credentials.Domain) ? credentials.UserName : $@"{credentials.Domain}\{credentials.UserName}";
var result = WNetAddConnection2(netResource, credentials.Password, userName, 0);
if (result != 0)
{
logger.Error(result + " UNC path error status code");
throw new Win32Exception(result, "Error connecting to remote share");
}
}
~ConnectToSharedFolder()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
WNetCancelConnection2(_networkName, 0, true);
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name, int flags, bool force);
[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
public enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};
public enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
}
public enum ResourceDisplaytype : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}
}
Код: Выделить всё
function downloadSourceDoc(sourceDocID) {
showLoader();
var form = $('form');
form.attr('action', '../DownloadSourceDoc?SourceDocID=' + sourceDocID + '&RoleID=' + $('#hdnUserRoleID').val());
form.attr('method', 'POST');
form.submit();
closeLoader();
}
- Я проверил следующее:
- Сетевые учетные данные верны.
- Путь доступен с сервера.
- Необходимые разрешения предоставлены.
- Нет создаются журналы ошибок.
- Просмотр событий не показывает никаких необычных записей.
[*]Функция загрузки работает из IIS.
[*]Загрузка работает с локального хоста, но не работает из IIS.
Подробнее здесь: https://stackoverflow.com/questions/788 ... -localhost